Cod sursa(job #1780064)
Utilizator | Bunea Andrei adystar00 | Data | 15 octombrie 2016 20:25:10 |
---|---|---|---|
Problema | Ferma | Scor | 0 |
Compilator | cpp | Status | done |
Runda | Arhiva de probleme | Marime | 1.76 kb |
#include <iostream>
#include <fstream>
#include <deque>
using namespace std;
deque <int> q,q2;
int v[10002];
int main()
{
ifstream fin ("ferma.in");
ofstream fout ("ferma.out");
int nr=0,k,i,n,pp=0;
long long sum=0,maxc=0;
fin>>n>>k;
for(i=1; i<=n; i++)
fin>>v[i];
for(i=1; i<=n; i++)
v[n+i]=v[i];
for(i=1; i<=2*n&&pp==0; i++)
{
sum=sum+v[i];
if(i>n&&(sum<0||v[i]<0))
pp=1;
else
{
if(sum>0)
{
if(sum>maxc)
maxc=sum;
}
else
{
if(maxc>0)
{
nr++;
while(!q.empty()&&q.back()>=maxc)
{
q.pop_back();
q2.pop_back();
}
q.push_back(maxc);
q2.push_back(nr);
if(nr>=k)
if(q.front()<nr-k+1)
{
q.pop_front();
q2.pop_front();
}
}
sum=0;
maxc=-1;
}
}
}
if(maxc>0)
{
nr++;
while(!q.empty()&&q.back()>=maxc)
{
q.pop_back();
q2.pop_back();
}
q.push_back(maxc);
q2.push_back(nr);
if(nr>=k)
if(q2.front()<nr-k+1)
{
q.pop_front();
q2.pop_front();
}
}
sum=0;
while(!q.empty())
{
sum+=q.front();
q.pop_front();
}
fout<<sum;
return 0;
}