Cod sursa(job #254243)

Utilizator pauldbPaul-Dan Baltescu pauldb Data 7 februarie 2009 02:23:42
Problema Cuburi2 Scor Ascuns
Compilator cpp Status done
Runda Marime 0.59 kb
#include <stdio.h>
#include <stdlib.h>

#define ll long long
#define inf 1000000000000000000LL
#define MAXN 250010

int N, M, P;
int A[MAXN];
ll best;

int main()
{
	freopen("cuburi2.in", "r", stdin);
	freopen("cuburi2.out", "w", stdout);

	int i, j, k, x, y;
	ll v;

	scanf("%d %d ", &N, &M);

	for (i = 1; i <= N; i++) scanf("%d ", &A[i]);

	for (i = 1; i <= M; i++) 
	{
		scanf("%d %d ", &x, &y);

		best = inf;

		for (j = x; j <= y; j++)
		{
			v = 0;
			for (k = x; k <= y; k++) v += 1LL * abs(j-k) * A[k];

			if (v < best) best = v, P = j;
		}

		printf("%d %lld\n", P, best);
	}

	return 0;
}