Cod sursa(job #1491964)

Utilizator AlexandraaaaMereu Alexandra Alexandraaaa Data 26 septembrie 2015 19:12:04
Problema Buline Scor 20
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.78 kb
#include <iostream>
#include <fstream>

using namespace std;

int main(){
	ifstream f("buline.in");
	ofstream g("buline.out");

	int n, s, i, l, smax, lmax, imax, x;
	int v[200005];

	f >> n;
	f >> v[1] >> x;
	if (x == 0)
		v[1] *= (-1);
	smax = s = v[1];
	l = i = imax = lmax = 1;

	for (int j = 2; j <= n; ++j){
		f >> v[j] >> x;
		if (x == 0)
			v[j] *= (-1);

		if (s < 0){
			s = v[j];
			i = j;
			l = 1;
		}
		else{
			s += v[j];
			l++;
		}

		if (s > smax){
			smax = s;
			imax = i;
			lmax = l;
		}
	}

	if (s > 0)
		for (int j = 1; j < i && s > 0; ++j){
			s += v[j];
			l++;
			if (s > smax){
				smax = s;
				imax = i;
				lmax = l;
			}
		}

	g << smax << " " << imax << " " << lmax;

	f.close();
	g.close();

	return 0;
}