Cod sursa(job #1184970)

Utilizator laurageorgescuLaura Georgescu laurageorgescu Data 14 mai 2014 18:53:41
Problema Buline Scor 20
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.79 kb
#include<fstream>

using namespace std;

ifstream fin( "buline.in" );
ofstream fout( "buline.out" );

const int nmax = 200000;
int v[ 2 * nmax + 1 ];

int main() {
    long long n, sol, x, s, p, l;
    fin>>n;
    for( int i = 1; i <= n; ++ i ) {
        fin>>s>>x;
        if ( x == 0 ) {
            v[ i ] = v[ i + n ] = -s;
        } else {
            v[ i ] = v[ i + n ] = s;
        }
    }
    s = sol = -1<<30;
    x = 0;
    for( int i = 1; i <= 2 * n; ++ i ) {
        if ( s > 0 && i - x + 1 <= n ) {
            s += v[ i ];
        } else {
            s = v[ i ];
            x = i;
        }
        if ( s > sol ) {
            sol = s;
            p = x;
            l = i - x + 1;
        }
    }
    fout<<sol<<' '<<p<<' '<<l<<'\n';
    fin.close();
    fout.close();
    return 0;
}