Cod sursa(job #2408137)

Utilizator tomaionutIDorando tomaionut Data 17 aprilie 2019 17:33:12
Problema Buline Scor 20
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.77 kb
#include <bits/stdc++.h>

using namespace std;

ifstream fin("buline.in");
ofstream fout("buline.out");
int a[400005],n;

int main()
{
    int i,x,y,s=0,smax=INT_MIN,st,dr,aux;
    fin >> n;
    for (i=1; i<=n; i++)
    {
        fin >> x >> y;
        if (y==1)
            a[i]=x;
        else a[i]=-x;
        a[i+n]=a[i];
    }
    aux=1;
    s=a[1];
    if (s<0)
        {
            s=0;
            aux=2;
        }
    for (i=2; i<=2*n; i++)
    {
        if (s<0 or i-st==n)
        {
            s=a[i];
            aux=i;
        }
        else s+=a[i];

        if (s>smax)
        {
            st=aux;
            dr=i;
            smax=s;
        }
    }
    fout << smax << " " << st << " " << dr-st+1 << "\n";

    return 0;
}