Cod sursa(job #3344893)

Utilizator EduardDobrescuEduard Dobrescu Cristian Gabriel EduardDobrescu Data 6 martie 2026 14:33:43
Problema Buline Scor 20
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.95 kb
#include <fstream>
#include <climits>
using namespace std;

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

int v[400001];
int n, i, st, dr, stCurent;
long long s, maxim;

int main()
{
    int x, c;
    fin >> n;
    for(i = 1; i <= n; i++){
        fin >> x >> c;
        if(c == 1)
            v[i] = x;
        else
            v[i] = -x;
        v[i+n] = v[i];
    }
    s = v[1];
    maxim = s;
    st = dr = stCurent = 1;
    for(i = 2; i <= 2*n; i++){
        if(s + v[i] >= v[i])
            s = s + v[i];
        else{
            s = v[i];
            stCurent = i;
        }
        if(i - stCurent + 1 > n){
            s -= v[stCurent];
            stCurent++;
        }
        if(s > maxim){
            maxim = s;
            st = stCurent;
            dr = i;
        }
    }
    int P = st;
    if(P > n) P -= n;
    int L = dr - st + 1;
    fout << maxim << " " << P << " " << L;
    return 0;
}