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
90
91
92
93
94
95
96
97
98
#include <cstdio>
#include "kanapka.h"
#include "message.h"

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

	long long n = GetN();
	long long segmentsPerNode = (n + nodesNo - 1) / nodesNo;

	long long sum = 0;
	long long bst = 0;
	long long wst = 0;
	long long wstres = 0;

	if (segmentsPerNode * id < n) {
		long long loopStop = segmentsPerNode * (id+1); 
		if (loopStop > n) loopStop = n;

		long long wstresnow = 0;
		for (long long i = segmentsPerNode * id; i < loopStop ; ++i) {
			long long taste;
			taste = GetTaste(i);
			sum += taste;
			if (sum > bst) {
				bst = sum;
			}
			if (sum < wst) {
				wst = sum;
			}
			if (wstresnow < 0) {
				wstresnow += taste;
			} else {
				wstresnow = taste;
			}

			if (wstresnow < wstres) {
				wstres = wstresnow;
			}
		}
	}

	if (id > 0) {
		PutLL(0, sum);
		PutLL(0, wstres);
		PutLL(0, sum-bst);
		PutLL(0, wst);
		Send(0);
	} else {
		long long totalSum = sum;
		long long totalWorst = 0;
		if (wstres < totalWorst)
			totalWorst = wstres;
		if (wst < totalWorst)
			totalWorst = wst;
		if (sum-bst < totalWorst)
			totalWorst = sum-bst;

		long long totalWstNow = sum-bst;

		for (int i = 1; i < nodesNo ; ++i) {
			Receive(i);
			long long SumI = GetLL(i);
			long long L_R = GetLL(i);
			long long L__ = GetLL(i);
			long long __R = GetLL(i);

			totalSum += SumI;

			if (L_R < totalWorst) {
				totalWorst = L_R;
			}

			if (totalWstNow > 0) {
				totalWstNow = 0;
			}

			if ((totalWstNow + __R) < totalWorst) {
				totalWorst = (totalWstNow + __R);
			}

			totalWstNow += SumI;
			if (totalWstNow > L__)
				totalWstNow = L__;

			if (totalWstNow < totalWorst) {
				totalWorst = totalWstNow;
			}
		}

		printf("%lld\n", totalSum - totalWorst);
	}
	
	return 0;
}

// http://potyczki.mimuw.edu.pl/media/attachment/rpa_linux.tar.bz2