Cod sursa(job #2788664)

Utilizator cadmium_Voicu Mihai Valeriu cadmium_ Data 26 octombrie 2021 11:17:55
Problema Avioane Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.37 kb
#include <fstream>
#include <algorithm>
#define int long long  

using namespace std;

ifstream cin("avioane.in");
ofstream cout("avioane.out");

struct func {
  int m,b;
  int operator() (int x) {
    return m*x+b;
  }  
  bool operator()(func x, func y) {
    if(y.m==m)
      return y.b>b;
    return (b-x.b)*(m-y.m)<(m-x.m)*(b-y.b);
  }
};

namespace CHT {
  func stack[100000];
  int stptr=0,ptr;
  void insert(func val) {
    if(stptr==0)
      stack[stptr++]=val;
    else {
      while(stptr>1 && val(stack[stptr-2],stack[stptr-1])) {
        //cout << "-" << stack[stptr-1].m << ' '<< stack[stptr-1].b <<'\n';
        stptr--;
      }
      if(stptr==1 && stack[0].b<val.b)
        stack[0]=val;
      else
        stack[stptr++]=val;
      //cout << "+" << stack[stptr-1].m << ' '<< stack[stptr-1].b <<'\n';
    }
  }
  int query(int x) {
    if(stptr==0)
      return 0;
    if(ptr>=stptr)
      ptr=stptr-1;
    while(ptr+1<stptr && stack[ptr](x)<stack[ptr+1](x))
      ptr++;
    return stack[ptr](x);
  }
};

int v[100000];

signed main() {
  int n;
  cin >> n;
  for(int i=0; i<n; i++)
    cin >> v[i];
  sort(v,v+n);
  int maxx=0;
  for(int i=0; i<n; i++) {
    if(v[i]!=v[i-1])
      CHT::insert(func{v[i],-v[i]*(i-1)});
    //cout << v[i] << ' '<< -v[i]*(i) <<' ' << i+1 << ' '<< (n-i)<< '*' <<v[i+1] << '\n';
    maxx=max(CHT::query(i)+(n-i-1)*v[i+1],maxx);
  }
  cout << maxx << '\n';
}