Cod sursa(job #2469500)

Utilizator Diana_IonitaIonita Diana Diana_Ionita Data 7 octombrie 2019 16:39:49
Problema Buline Scor 20
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.79 kb
#include <bits/stdc++.h>

using namespace std;
ifstream fin("buline.in");
ofstream fout("buline.out");
int n,i,b,a,poz,pozm,lm,l,v[400002];
long long smax,best[400002];
int main()
{
    fin>>n;
    for(i=1; i<=n; i++)
    {
        fin>>a>>b;
        if(b==0)
        {
            v[i]=v[n+i]=-a;

        }
        else v[i]=v[n+i]=a;
    }
    smax=v[1];

    n=2*n;
    poz=1;
    l=1;

    for(i=1; i<=n; i++)
    {
        best[i]=v[i];
        if(best[i]<best[i-1]+v[i])
        {
            if(l<n/2)best[i]=best[i-1]+v[i],l++;
            else best[i]=v[i];
        }
        else
        {
            poz=i;
            l=1;
        }


        if(smax<best[i]&&l<n) smax=best[i],pozm=poz,lm=l;
    }
    fout<<smax<<" "<<pozm<<" "<<lm;
    return 0;
}