Cod sursa(job #3368319)

Utilizator PortallosAlexandru Vladimir Portallos Data 14 octombrie 2026 11:07:18
Problema Buline Scor 20
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.26 kb
#include <bits/stdc++.h>

using namespace std;

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

int n;
int a[200001];
int dp, dp1;
int curStart, curLen;
int Max = INT_MIN, startMax, lenMax;

int main() {

    fin>>n;
    for(int i = 1; i <= n; i++){
        int x, b;
        fin>>x>>b;

        if(b)
            a[i] = x;
        else
            a[i] = -x;
    }

    for(int j = 1; j <= n * 2; j++){
        int i;
        if(j > n)
            i = j - n;
        else
            i = j;


        if(curLen < n){
            if(a[i] < a[i] + dp1){
                dp = a[i] + dp1;
                curLen++;
            }
            else {
                dp = a[i];
                curStart = i;
                curLen = 1;
            }
        }
        else{
            if(a[i] < a[i] + dp1 - a[j-curLen]){
                dp = a[i] + dp1 - a[j-curLen];
            }
            else {
                dp = a[i];
                curStart = i;
                curLen = 1;
            }
        }


        if(Max < dp){
            Max = dp;
            lenMax = curLen;
            startMax = curStart;
        }

        dp1 = dp;
    }

    fout<<Max<<" "<<startMax<<" "<<lenMax;

    return 0;
}