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
#include "kanapka.h"
#include "message.h"
#include <algorithm>
#include <iostream>

using namespace std;

long long n;
long long myID;

int main() {
	long long nodes = NumberOfNodes();
	myID = MyNodeId();

  	n = GetN();
  	int from, to;
  	from = n * myID / nodes;
  	to = n * (myID + 1) / nodes;
	
	long long minMid = 0, minPref = 0, minSuff = 0, sum = 0;
	for (int i = from; i < to; i++) {
		int taste = GetTaste(i);
		sum += taste;
		minSuff = min(minSuff + taste, 0ll);
		minPref = min(minPref, sum);
		minMid = min(minMid, minSuff);
	}

	switch (myID) {
		case 0: {
			for (int i = 1; i < nodes; i++) {
				long long suff, pref, s, mid;
				Receive(i);
				s = GetLL(i);
				pref = GetLL(i);
				suff = GetLL(i);
				mid = GetLL(i);
				minMid = min(min(minMid, mid), minSuff + pref);
				minSuff = min(minSuff + s, suff);
				minPref = min(minPref, sum + pref);
				sum += s;
			}
			cout << (sum - minMid > 0ll ? sum - minMid : 0ll) << "\n";
			break;
		} 
		default: {
			PutLL(0, sum);
			PutLL(0, minPref);
			PutLL(0, minSuff);
			PutLL(0, minMid);
			Send(0);
		}
	}
	return 0;
}