Cod sursa(job #2699421)

Utilizator CosminMorarMorar Cosmin Andrei CosminMorar Data 24 ianuarie 2021 14:01:04
Problema Avioane Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.32 kb
#include <bits/stdc++.h>
#define FASTIO fin.tie(0), fout.tie(0), ios::sync_with_stdio(0)
using namespace std;
using ll = long long;
using ld = long double;
using ull = unsigned long long;

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

ll n, cost[100100], asc[100100];

int main() {
	FASTIO;
	fin >> n;
	for (int i = 1; i <= n; i++)
		fin >> cost[i];
	sort(cost + 1, cost + n + 1);
	
	int asc_len = 0;
	for (int i = 1; i <= n; i++)
		if (cost[i] > cost[asc[asc_len]])
			asc[++asc_len] = i;
	
	vector<pair<int, ll>> best; /// pos, time it beats the ticket before
	best.emplace_back(1, 2);
	for (int x = 2; x <= asc_len; x++) {
		ll i, j, t;
		j = asc[x];
		
		while (best.size() > 1) {
			i = best.back().first;
			t = (cost[j] + (j - i - 1) * cost[i] - 1) / (cost[j] - cost[i]);
			if (j + t > best.back().second)
				break;
			best.pop_back();
		}
		
		i = best.back().first;
		t = (cost[j] + (j - i - 1) * cost[i] - 1) / (cost[j] - cost[i]);
		best.emplace_back(j, j + t);
	}
	
	ll cost_max = 0;
	ll other_pos = 0;
	for (int j = 2; j <= n; j++) {
		if (other_pos < (int)best.size() - 1)
			if (j == best[other_pos + 1].second) 
				other_pos++;
		
		int i = best[other_pos].first;
		ll cost_act = 1LL * (n - j + 1) * cost[j] + 1LL * (j - i) * cost[i];
		cost_max = max(cost_max, cost_act);
	}
	
	fout << cost_max << '\n';
	return 0;
}