Cod sursa(job #3202475)

Utilizator hhhhhhhAndrei Boaca hhhhhhh Data 11 februarie 2024 17:08:00
Problema Avioane Scor 10
Compilator cpp-64 Status done
Runda Concurs for fun Marime 1.62 kb
#include <bits/stdc++.h>

using namespace std;
ifstream fin("avioane.in");
ofstream fout("avioane.out");
mt19937 rng(41289);
typedef long long ll;
const ll INF=2e5;
ll n,v[100005];
struct func
{
    ll a,b,l,r;
};
ll intersect(func f, func g)
{
    if(f.a==g.a)
    {
        if(f.b>g.b)
            return 1;
        return INF;
    }
    ll x=(g.b-f.b)/(f.a-g.a);
    while(f.a*x+f.b<g.a*x+g.b)
        x++;
    return x;
}
vector<func> cht;
void add(ll a,ll b)
{
    func f={a,b,0,0};
    while(!cht.empty())
    {
        func g=cht.back();
        ll x=intersect(f,g);
        if(x>g.r)
            return;
        if(x<=g.l)
        {
            cht.pop_back();
            continue;
        }
        g.r=x-1;
        f.l=x;
        f.r=INF;
        cht.pop_back();
        cht.push_back(g);
        cht.push_back(f);
        return;
    }
    if(cht.empty())
    {
        cht.push_back({a,b,1,INF});
        return;
    }
}
ll getbest(ll x)
{
    ll st=0;
    ll dr=cht.size();
    dr--;
    while(st<=dr)
    {
        ll mij=(st+dr)/2;
        if(cht[mij].l<=x&&cht[mij].r>=x)
            return x*cht[mij].a+cht[mij].b;
        if(cht[mij].l>x)
            dr=mij-1;
        else
            st=mij+1;
    }
    return 0;
}
int main()
{
    ios_base::sync_with_stdio(false);
    fin.tie(0);
    fin>>n;
    for(int i=1;i<=n;i++)
        fin>>v[i];
    sort(v+1,v+n+1);
    ll ans=0;
    for(ll i=1;i<=n;i++)
    {
        add(v[i],-i*v[i]);
        ll val=getbest(i);
        val+=v[i]*(n-i+1);
        ans=max(ans,val);
    }
    fout<<ans;
    return 0;
}