Cod sursa(job #1720417)

Utilizator BrandonChris Luntraru Brandon Data 22 iunie 2016 14:52:34
Problema Avioane Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.88 kb
#include <fstream>
#include <algorithm>

#define LL long long
using namespace std;

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

const int MaxN = 100005;

LL Ans;
int v[MaxN];
int n;

inline LL Cost(int B, int E) {
  return (LL) (B - E) * v[E] + (LL) (n - B + 1) * v[B];
}

void Solve(LL &ans, int LfB = 1, int RgB = n, int LfE = 1, int RgE = n) {
  if (LfB > RgB) return;

  int CurrB = (LfB + RgB) / 2;
  int BestE = LfE;

  for (int i = LfE + 1; i <= min(CurrB - 1, RgE); ++i) {
    if (Cost(CurrB, i) > Cost(CurrB, BestE)) {
      BestE = i;
    }
  }

  ans = max(ans, Cost(CurrB, BestE));
  Solve(ans, LfB, CurrB - 1, LfE, BestE);
  Solve(ans, CurrB + 1, RgB, BestE, RgE);
}

int main() {
  cin >> n;
  for (int i = 1; i <= n; ++i) {
    cin >> v[i];
  }

  sort(v + 1, v + n + 1);
  Solve(Ans);
  cout << Ans << '\n';
  return 0;
}