Cod sursa(job #1216229)

Utilizator toranagahVlad Badelita toranagah Data 3 august 2014 19:35:28
Problema Avioane Scor 50
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.82 kb
//Problem avioane from Infoarena
#include <cmath>
#include <algorithm>
#include <deque>
#include <fstream>
#include <iostream>
#include <string>
#include <vector>
using namespace std;

ifstream fin("avioane.in");
ofstream fout("avioane.out");

const int INF = 1 << 30;
const int MAX_N = 100100;

int N;
int v[MAX_N];
deque<int> dq;

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

  sort(v + 1, v + N + 1);
  dq.push_back(0);

  int result = -INF;
  for (int b = 1; b <= N; b += 1) {
    int best = -INF;
    for (auto e: dq) {
      best = max(best, (b - e) * v[e] + (N - b + 1) * v[b]);
    }
    result = max(result, best);

    while (!dq.empty() && best > (b - dq.front()) * v[dq.front()] + (N - b + 1) * v[b])
      dq.pop_front();
   
    dq.push_back(b);
  }
  fout << result;
  return 0;
}