Cod sursa(job #3248786)

Utilizator prares06Papacioc Rares-Ioan prares06 Data 13 octombrie 2024 11:12:44
Problema Buline Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.03 kb
#include<bits/stdc++.h>
using namespace std;

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

int n, x, semn, poz, lmax, smax = -1e9;
queue<int> q;

int main(){
    fin >> n;

    for(int i = 1; i <= n; ++i){
        fin >> x >> semn;
        if(!semn)
            x = -x;
        q.push(x);
    }

    int istart = 1, s = 0, len = 1;

    for(int i = 1; i <= 2 * n; ++i){
        x = q.front();
        q.pop();
        q.push(x);
        s += x;
        if(s > smax or (s == smax and istart < poz) or (s == smax and istart == poz and len < lmax)){
            poz = istart;
            smax = s;
            lmax = len;
        }
        if(s < 0 or len > n){
            s = 0;
            len = 1;
            istart = i + 1;
        }
        else{
            ++len;
        }
    }
    if(s > smax or (s == smax and istart < poz) or (s == smax and istart == poz and len < lmax)){
        poz = istart;
        smax = s;
        lmax = len;
    }

    fout << smax << ' ' << poz << ' ' << len;
}