Cod sursa(job #2224342)

Utilizator oldatlantianSerban Cercelescu oldatlantian Data 23 iulie 2018 19:04:42
Problema Avioane Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.44 kb
#include <bits/stdc++.h>
using namespace std;

ifstream fi("avioane.in");
ofstream fo("avioane.out");

using i64 = long long;

const int N = 1e5 + 5;

struct Line {
	i64 a, b;
	inline i64 operator () (const i64 &x) const {
		return a * x + b; } };

struct LiChao {
	vector<Line> pom;
	int n, qx;

	LiChao(int _n) : n(_n), pom(_n * 4, Line {0, i64(-1e18)}) { }

	void update(int nod, int st, int dr, Line updl) {
		Line &now = pom[nod];
		if (now(st) >= updl(st) && now(dr) >= updl(dr))
			return;
		if (now(st) <= updl(st) && now(dr) <= updl(dr)) {
			now = updl;
			return; }

		int mid = (st + dr) / 2;
		if (now(st) <= updl(st))	
			swap(now, updl);
		if (now(mid) <= updl(mid)) {
			swap(now, updl);
			update(2 * nod, st, mid, updl); }
		else
			update(2 * nod + 1, mid + 1, dr, updl); }

	void update(i64 a, i64 b) {
		update(1, 1, n, {a, b}); }

	i64 query(int nod, int st, int dr) {
		if (st == dr) return pom[nod](qx);
		i64 ant = pom[nod](qx);
		int mid = (st + dr) / 2;

		return qx <= mid ? max(ant, query(2 * nod, st, mid)) : max(ant, query(2 * nod + 1, mid + 1, dr)); }

	i64 query(int _qx) {
		qx = _qx;
		return query(1, 1, n); } };


i64 v[N];

LiChao *batch;
i64 ant;
int n;


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

	batch = new LiChao(n);
	sort(v + 1, v + 1 + n);

	for (int i = 1; i <= n; ++i) {
		batch->update(v[i], -i * v[i]);
		ant = max(ant, batch->query(i) + (n - i + 1) * v[i]); }

	fo << ant << endl;

	return 0; }