Cod sursa(job #2384176)

Utilizator toonewbieAbutalib Namazov toonewbie Data 20 martie 2019 14:32:23
Problema Avioane Scor 80
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.67 kb
#include <bits/stdc++.h>
#define endl '\n'
#define ll long long

using namespace std;

const int N = 100005;

struct line{
    int k;ll b;
    long long get(ll x) {
        return 1LL * k * x + b;
    }
};

struct node{
    node *l, *r;
    line x;
    node(node* L, node* R, line X): l(L), r(R), x(X) {};
};
typedef node* pnode;

void add(pnode &t, ll l, ll r, line x) {
    if (t == NULL) t = new node(NULL, NULL, x);
    ll mid = (l + r) >> 1LL;
    ll vtm = (t -> x).get(mid);
    ll vxm = x.get(mid);
    ll vtl = t -> x.get(l);
    ll vxl = x.get(l);
    int isl = vxl > vtl, isr = vxm > vtm;
    if (vxm > vtm) swap(t -> x, x);
    if (r - l == 1) return;
    if (isl != isr) add(t -> l, l, mid, x);
    else add(t -> r, mid, r, x);
}
long long get(pnode &t, ll l, ll r, ll x) {
    if (t == NULL) return 0;
    ll mid = (l + r) >> 1LL;
    if (r - l == 1) {
        return t -> x.get(x);
    } else if (x < mid) {
        return max(get(t -> l, l, mid, x), t -> x.get(x));
    }
    return max(get(t -> r, mid, r, x), t -> x.get(x));
}

const ll LIM = 100000000000500;

int n, arr[N];
pnode t;
int main() {
    freopen("avioane.in", "r", stdin);
    freopen("avioane.out", "w", stdout);
    ios_base :: sync_with_stdio(0);
    cin.tie(0), cout.tie(0);
    cin >> n;
    for (int i = 1; i <= n; i++) {
        cin >> arr[i];
    }
    sort(arr + 1, arr + n + 1);
    ll res = 1LL * n * arr[1];
    add(t, 1, LIM, {arr[1], arr[1]});
    for (int i = 2; i <= n; i++) {
        res = max(res, 1LL * (n - i + 1) * arr[i] + get(t, 1, LIM, i));
        add(t, 1, LIM, {arr[i], -1LL * arr[i] * i});
    }
    cout << res << endl;
    return 0;
}