Cod sursa(job #3332509)

Utilizator Costy2345Costi Dimian Costy2345 Data 7 ianuarie 2026 00:51:32
Problema Buline Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.37 kb
#include <bits/stdc++.h>
#define int long long
using namespace std;

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

const int NMAX = 1e5 * 2 + 2;

int n, v[NMAX], sumtot = 0;

signed main()
{
    fin >> n;

    for(int i = 1; i <= n; i++)
    {
        int x, y;
        fin >> x >> y;
        v[i] = y ? x : x * (-1);
        sumtot += v[i];
    }
    int l = 1, r = 1, curl = 1, max = v[1], curmax = v[1];
    for(int i = 2; i <= n; i++)
    {
        if(curmax + v[i] > v[i])
        {
            curmax += v[i];
        }
        else{
            curmax = v[i];
            curl = i;
        }
        if(curmax > max)
        {
            max = curmax;
            l = curl;
            r = i;
        }
    }
    int ans = max, lf = l, rf = r, len = r - l + 1;

    l = 1, r = 1, curl = 1, max = v[1], curmax = v[1];
    for(int i = 2; i <= n; i++)
    {
        if(curmax + v[i] < v[i])
        {
            curmax += v[i];
        }
        else{
            curmax = v[i];
            curl = i;
        }
        if(curmax < max)
        {
            max = curmax;
            l = curl;
            r = i;
        }
    }

    max = sumtot - max;
    if(max > ans)
    {
        ans = max;
        lf = r + 1;
        len = n - (r - l + 1);
    }
    fout << ans << " " << lf << " " << len;

    return 0;
}