Cod sursa(job #1836516)

Utilizator cella.florescuCella Florescu cella.florescu Data 28 decembrie 2016 14:17:04
Problema Avioane Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.9 kb
#include <bits/stdc++.h>

using namespace std;

const int MAXN = 1e5;

long long ans;
int n;
int v[MAXN];

inline long long compute(int ecpos, int bupos) {
  return 1LL * v[ecpos] * (bupos - ecpos) + 1LL * v[bupos] * (n - bupos);
}

void profit(int bbegin, int bend, int ebegin, int eend) {
  if (bbegin > bend)
    return;
  int mid = (bbegin + bend) / 2, pos = ebegin;
  for (int i = ebegin + 1; i <= eend; ++i)
    if (compute(i, mid) > compute(pos, mid))
      pos = i;
  if (compute(pos, mid) > ans)
    ans = compute(pos, mid);
  profit(bbegin, mid - 1, ebegin, pos);
  profit(mid + 1, bend, pos, eend);
}

int main()
{
    ifstream fin("avioane.in");
    fin >> n;
    for (int i = 0; i < n; ++i)
      fin >> v[i];
    fin.close();
    sort(v, v + n);
    profit(0, n - 1, 0, n - 1);
    ofstream fout("avioane.out");
    fout << ans << '\n';
    fout.close();
    return 0;
}