Cod sursa(job #2767565)

Utilizator OvidRata Ovidiu Ovid Data 6 august 2021 19:02:40
Problema Euro Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.77 kb
#include<bits/stdc++.h>
using namespace std;
#define INIT  ios_base :: sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
#define mp make_pair
#define pb push_back
#define ft first
#define sc second
#define ll long long
#define pii pair<int, int>
#define count_bits __builtin_popcount
#define int ll



ifstream fin("euro.in"); ofstream fout("euro.out");
#define cin fin
#define cout fout



int t, n,a[35001];
int s[35001];
int dp[35000];


struct nod{
int a, b;

nod *l, *r;
nod(int a, int b){
    this->a=a;
    this->b=b;
    this->l=this->r=NULL;
}


} *li_chao;





void update(nod *&t, int l, int r, int a, int b){

if(t==NULL){
    t=new nod(a, b);
    return;
}

if( ((t->a*l+t->b)>(a*l+b)) && ( (t->a*r+t->b)>(a*r+b) )   ){
    return;
}

if( ((t->a*l+t->b)<=(a*l+b)) && ( (t->a*r+t->b)<=(a*r+b) )   ){
    t->a=a; t->b=b;
    return;
}


int mid=(l+r)>>1ll;

update(t->l, l, mid, a, b); update(t->r, mid+1, r, a, b);
return;
}




int query(nod *t, int l, int r, int x){

if(r==l){
    return x*t->a+t->b;
}
int mid=(l+r)>>1ll;
if(x<=mid){
    if(!t->l){
        return t->a*x+t->b;
    }
    return max( t->a*x+t->b,   query(t->l, l, mid, x)   );
}
else{
    if(!t->r){
        return t->a*x+t->b;
    }
    return max(t->a*x+t->b, query(t->r, mid+1, r, x)  );
}


}




int32_t main(){
INIT
cin>>n>>t;
for(int i=1; i<=n; i++){
    cin>>a[i];
    s[i]=s[i-1]+a[i];
}
dp[0]=0;
//initial, structura li_chao va contine doar o funcite (0, 0);
update(li_chao, 1, n, -s[0], dp[0] );

for(int i=1; i<=n; i++){
    dp[i]=query(li_chao, 1, n, i)-t+i*s[i];
    update(li_chao, 1, n, -s[i], dp[i]);
}
cout<<dp[n];


return 0;
}