Cod sursa(job #455221)

Utilizator popoiu.georgeGeorge Popoiu popoiu.george Data 13 mai 2010 10:57:15
Problema Buline Scor 80
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.02 kb
#include<fstream>
#define inf "buline.in"
#define outf "buline.out"
#define NMax 400100
#define INF 100000000000000000LL
using namespace std;

fstream f(inf,ios::in),g(outf,ios::out);

int N;
long long v[NMax],v2[NMax];
long long sum;

void read()
{
int a,b;
f>>N;
for(int i=1; i<=N; i++)
    {
    f>>a>>b;
    if( !b ) v[i]=-a;
    else v[i]=a;
    v2[i]=(-1)*v[i];
    sum += (v[i]);
    }
}

void scmax(long long a[],int &pm,int &lm,long long &smax)
{
int p,l;
long long s=-INF;
for(int i=1; i<=N; i++)
    {
    if( s+a[i]<a[i] )
        {
        s = a[i];
        p = i;
        l = 1;
        }
    else s += (a[i]), l++;
    if( s>smax ) smax=s, pm=p, lm=l;
    else if( s==smax )
        {
        if( p+l<pm+lm ) pm=p , lm=l;
        else if( p+l==pm+lm && N-l<N-lm ) pm=p,lm=l;
        }
    }
}

void solve()
{
int p,l;
long long s=-INF;
scmax(v2,p,l,s);
//g<<s<<"\n";
g<< (long long)(sum+s) <<" "<< p+l <<" "<< N-l ;
}

int main()
{
read(); solve();
f.close(); g.close();
return 0;
}