Cod sursa(job #2406202)

Utilizator NeganAlex Mihalcea Negan Data 15 aprilie 2019 14:57:29
Problema Buline Scor 20
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.78 kb
#include <bits/stdc++.h>

using namespace std;
ifstream fin("buline.in");
ofstream fout("buline.out");
int n, a[400010];
int main()
{
    int i, s, smax, x, y, p, st, dr;
    fin >> n;
    for(i = 1;i <= n;i++)
    {
        fin >> x >> y;
        if(y)
            a[i] = a[n + i] = x;
        else
            a[i] = a[n + i] = -1 * x;
    }
    s = smax = a[1];
    if(s < 0)
    {
        s = 0;
        p = 2;
    }
    for(i = 2;i <= 2 * n;i++)
    {
        s += a[i];
        if(s > smax && i - p + 1 <= n)
        {
            smax = s;
            st = p;
            dr = i;
        }
        if(s < 0)
        {
            s = 0;
            p = i + 1;
        }
    }

    fout << smax << " " << st << " " << dr - st + 1;

    return 0;
}