Cod sursa(job #221844)

Utilizator mISHOOOmISHOOO mISHOOO Data 18 noiembrie 2008 16:02:06
Problema Secventa 2 Scor 40
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.69 kb
#include <stdio.h>
#define NMAX 50000

FILE *fi = fopen("secv2.in", "r");
FILE *fo = fopen("secv2.out", "w");

int V[NMAX], S[NMAX], M[NMAX], P[NMAX];
int N, K, L=0, A, B, min, i, j;

int main() {
	fscanf(fi, "%d%d", &N, &K);
	fscanf(fi, "%d", &V[0]);
	min = V[0];
	M[0] = min;
	P[0] = 0;
	S[0] = V[0];

	for (i=1; i<N; i++) {
		fscanf(fi, "%d", &V[i]);
		S[i] = S[i-1]+V[i];

		if (S[i]<min) {
			min = S[i];
			P[i] = i;
		}
		else P[i] = P[i-1];

		M[i] = min;			
	}

	fclose(fi);

	for (i=0; i<N; i++) {
		if (S[i]-M[i]>L && i-P[i]+1>=K) {
			L = S[i] - M[i];
			B = i+1;
			A = P[i]+2;
		}
	}

	fprintf(fo, "%d %d %d\n", A, B, L);
	fclose(fo);
	return 0;
}