Cod sursa(job #1217924)

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

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

const int64_t INF = 1LL << 60;
const int MAX_N = 100100;

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

inline int get_b(int e1, int e2) {
  return floor((1.0 * (e1 * v[e1] - e2 * v[e2])) / (1.0 * v[e1] - v[e2]));
}

inline int64_t calc(int e, int b) {
  return 1LL * (b - e) * v[e] + 1LL * (N - b + 1) * v[b];
}

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

  sort(v + 1, v + N + 1);

  int64_t result = -INF;
  dq.push_back(1);
  for (int b = 2; b <= N; b += 1) {
    while (dq.size() >= 2 && get_b(dq[0], dq[1]) <= b)
      dq.pop_front();

    result = max(result, calc(dq.front(), b));

    while (dq.size() >= 2 && get_b(dq[dq.size() - 2], dq.back()) > get_b(dq.back(), b))
      dq.pop_back();

    dq.push_back(b);
  }

  fout << result;
  return 0;
}