Cod sursa(job #2403730)

Utilizator popabogdanPopa Bogdan Ioan popabogdan Data 11 aprilie 2019 20:08:23
Problema Avioane Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.92 kb
#include <bits/stdc++.h>

#define Nmax 100005
#define ll long long

using namespace std;

class InParser {
private:
	FILE *fin;
	char *buff;
	int sp;

	char read_ch() {
		++sp;
		if (sp == 4096) {
			sp = 0;
			fread(buff, 1, 4096, fin);
		}
		return buff[sp];
	}

public:
	InParser(const char* nume) {
		fin = fopen(nume, "r");
		buff = new char[4096]();
		sp = 4095;
	}

	InParser& operator >> (int &n) {
		char c;
		while (!isdigit(c = read_ch()) && c != '-');
		int sgn = 1;
		if (c == '-') {
			n = 0;
			sgn = -1;
		} else {
			n = c - '0';
		}
		while (isdigit(c = read_ch())) {
			n = 10 * n + c - '0';
		}
		n *= sgn;
		return *this;
	}

	InParser& operator >> (long long &n) {
		char c;
		n = 0;
		while (!isdigit(c = read_ch()) && c != '-');
		long long sgn = 1;
		if (c == '-') {
			n = 0;
			sgn = -1;
		} else {
			n = c - '0';
		}
		while (isdigit(c = read_ch())) {
			n = 10 * n + c - '0';
		}
		n *= sgn;
		return *this;
	}
};

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

int N;
int H[Nmax];
ll ans = 0;
pair <ll, ll> S[Nmax];
int L;
int p = 1;

bool bad(pair <ll, ll> L1, pair <ll, ll> L2, pair <ll, ll> L3)
{
    return 1ll * (L3.second - L1.second) * (L1.first - L2.first) < 1ll * (L2.second - L1.second) * (L1.first - L3.first);
}

int main()
{
    fin >> N;
    for(int i = 1; i <= N; i++)
        fin >> H[i];
    sort(H + 1, H + N + 1);
    ans = H[1] * N;
    for(int i = 1; i <= N; i++)
    {
        while(L >= 2 && bad(S[L - 1], S[L], make_pair(H[i], -1ll * (i - 1) * H[i])))
            L--;
        S[++L] = make_pair(H[i], -1ll * (i - 1) * H[i]);
        p = min(p, L);
        while(p < L && 1ll * S[p].first * i + S[p].second < 1ll * S[p + 1].first * i + S[p + 1].second)
            p++;
        ans = max(ans, 1ll * H[i + 1] * (N - i) + 1ll * S[p].first * i + S[p].second);
    }
    fout << ans << "\n";
    return 0;
}