1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#include <cstdio>
#include "kanapka.h"
#include "message.h"

long long sumaTab[1024], lewoTab[1024], prawoTab[1024], minTab[1024];

int main()
{
	int nodes = NumberOfNodes();
	int id = MyNodeId();

	long long N = GetN();

	int start = (int)(id * N / nodes);
	int end = (int)((id + 1) * N / nodes);

	long long suma = 0, lewo = 0, prawo = 0, min = 0, t = 0;

	for (int i = start; i < end; i++)
	{
		long long a = GetTaste(i);
		suma += a;
		if (suma < lewo)
			lewo = suma;

		if (t > 0)
			t = 0;
		t += a;
		if (t < min)
			min = t;
	}

	suma = 0;
	for (int i = end - 1; i >= start; i--)
	{
		long long a = GetTaste(i);
		suma += a;
		if (suma < prawo)
			prawo = suma;
	}

	if (id == 0)
	{
		sumaTab[0] = suma;
		lewoTab[0] = lewo;
		prawoTab[0] = prawo;
		minTab[0] = min;

		for (int i = 1; i < nodes; i++)
		{
			Receive(i);
			sumaTab[i] = GetLL(i);
			lewoTab[i] = GetLL(i);
			prawoTab[i] = GetLL(i);
			minTab[i] = GetLL(i);

			if (minTab[i] < min)
				min = minTab[i];

			suma += sumaTab[i];
 		}

		for (int i = 0; i < nodes - 1; i++)
		{
			long long s = 0;
			for (int j = i + 1; j < nodes; j++)
			{
				long long tmp = prawoTab[i] + s + lewoTab[j];
				if (tmp < min)
					min = tmp;

				s += sumaTab[j];
			}
		}

		printf("%lld", suma - min);
	}
	else
	{
		PutLL(0, suma);
		PutLL(0, lewo);
		PutLL(0, prawo);
		PutLL(0, min);
		Send(0);
	}

	//printf("%d %d %d\n", id, start, end);
    return 0;
}