Pagini recente » Cod sursa (job #728540) | Cod sursa (job #1493631) | Cod sursa (job #1652883) | Cod sursa (job #2201763) | Cod sursa (job #1943549)
#include <iostream>
#include <fstream>
using namespace std;
const int nmax=34567+5;
struct segm
{
long long a,b;
}st[nmax],dr;
long double a1,a2,b1,b2;
long long sum[nmax],best[nmax];
long long T,ind;
int n,i,pu,u,poz,ret,x;
long double intersect(segm unu,segm doi)
{
a1=unu.a;a2=doi.a;b1=unu.b;b2=doi.b;
return ((b2-b1)/(a1-a2));
}
int cb(long double val)
{
int ret=0;
if(u==1) return 1;
for(pu=17;pu>=0;pu--)
if(ret+(1<<pu)<u&&intersect(st[ret+(1<<pu)],st[ret+(1<<pu)+1])<val)
ret+=(1<<pu);
ret++;
return ret;
}
int main()
{
ifstream f("euro.in");
ofstream g("euro.out");
f>>n>>T;
for(i=1;i<=n;i++)
{
f>>x;
sum[i]=sum[i-1]+x;
}
for(i=n;i>=1;i--)
{
ind=i;
dr.a=-ind;
dr.b=1LL*(1LL*sum[i]*ind+best[i+1]-T);
while(u>=1&&intersect(st[u-1],st[u])>intersect(st[u-1],dr))
u--;
u++;
st[u]=dr;
poz=cb(sum[i-1]);
best[i]=1LL*(1LL*sum[i-1]*st[poz].a+st[poz].b);
}
g<<best[1];
return 0;
}