Cod sursa(job #586705)

Utilizator perticas_catalinperticas catalin perticas_catalin Data 2 mai 2011 20:01:21
Problema Avioane Scor 50
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.3 kb
#include <iostream>
#include <cstring>
#include <algorithm>

using namespace std;

#define NM 100005
#define LL long long
#define inf 9223372036854775807LL

int N, V[NM], dim;
LL a[NM], b[NM], s[NM], e[NM];

int main()
{
    freopen ("avioane.in", "r", stdin);
    freopen ("avioane.out", "w", stdout);

    scanf ("%d", &N);

    for (int i = 1; i <= N; ++i) scanf ("%d", &V[i]);

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

    ++dim;

    a[dim] = b[dim] = s[dim] = 0;
    e[dim] = inf;

    for (int i = 1; i <= N; ++i)
    {
        LL ac = V[i];
        LL bc = (LL)(1-i) * V[i];

        while (dim)
        {
            LL mom = (long double)(b[dim]-bc)/(ac-a[dim]) + 1;
            if (mom <= s[dim])
            {
                --dim;
                continue;
            }
            else
            {
                e[dim] = mom - 1;
                ++dim;
                a[dim] = ac, b[dim] = bc, s[dim] = mom, e[dim] = inf;
                break;
            }
        }
    }

    //for (int i = 1; i <= dim; ++i) printf ("%lld %lld %lld %lld\n", a[i], b[i], s[i], e[i]);

    LL ans = 0;

    int poz = 1;

    for (int i = 1; i <= N; ++i)
    {
        if (e[poz] < i) ++poz;

        LL ansc = a[poz] * i + b[poz] + (LL)(N - i) * V[i+1];
        ans = max (ans, ansc);
    }

    printf ("%lld", ans);

    return 0;
}