Cod sursa(job #503688)

Utilizator toniobFMI - Barbalau Antonio toniob Data 24 noiembrie 2010 13:52:47
Problema Buline Scor 20
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.82 kb
#include <fstream>
using namespace std;

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

const int N = 1 << 19;
int n, v[N], sumatut;

void citire () {
	in >> n;
	for (int i = 1, x; i <= n; ++i) {
		in >> v[i] >> x;
		v[i] = !x ? -v[i] : v[i];
		v[n + i] = v[i];
		sumatut += v[i];
	}
}

void exe () {
	int sc = v[1], smax = v[1], lc = 1, inceput = 1, p = 1, l = 1;
	
	for (int i = 2; i <= (n << 1); ++i) {
		if (sc < 0) {
			sc = v[i];
			inceput = i;
			lc = 1;
		} else {
			sc += v[i];
			++lc;
		}
		
		if (lc == n) {
			break;
		}
		
		if (sc > smax) {
			smax = sc;
			l = lc;
			p = inceput;
		}
	}
	
	if (l < n) {
		out << smax << ' ' << p << ' ' << l;
	} else {
		out << sumatut << " 1 " << n << "\n";
	}
}

int main () {
	citire ();
	
	exe ();
	
	return 0;
}