Cod sursa(job #2390924)

Utilizator alexboldasAlex Boldas alexboldas Data 28 martie 2019 14:55:04
Problema Buline Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.88 kb
#include <fstream>
#include <algorithm>
#include <iostream>
#include <cmath>
#include <bitset>
#include <string>
#include <climits>
using namespace std;
ifstream fin("buline.in");
ofstream fout("buline.out");
int x[500005];
int main()
{
    int n,a,sint,smax=INT_MIN,ps,pd,pint=1,s,smax1=INT_MIN;
    fin>>n>>x[1]>>a;if(a==0)x[1]-=2*x[1];
    sint=x[1];x[1+n]=x[1];s=x[1];
    for(int i=2;i<=n;i++)
        {fin>>x[i]>>a;if(a==0)x[i]-=2*x[i];s+=x[i];}
    for (int i=2;i<=n;i++)
    {
        if(sint<0)
            sint=x[i],pint=i;
        else sint+=x[i];
        if(smax<sint)
            smax=sint,ps=pint,pd=i;
    }
    pd-=ps-1;
    for(int i=1;i<=n;i++)
        x[i]-=2*x[i];
    sint=x[1];
    for (int i=2;i<=n;i++)
    {
        if(sint<0)
            sint=x[i];
        else sint+=x[i];
        if(smax1<sint)
            smax1=sint;
    }
    fout<<max(smax,s+smax1);
    return 0;

}