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

using namespace std;
typedef long long int ll;

int main()
{
	const int N = GetN();
	const int number_of_nodes = NumberOfNodes();
	const int part_size = N / number_of_nodes;
	const int remaining = N % number_of_nodes;

	/* for current node */
	const int node_id = MyNodeId();
	const int begin = node_id == 0 ? 0 : (node_id * part_size + remaining);
	const int end = (node_id + 1) * part_size + remaining;
	int i;

	ll total = 0, min_best = 0, current = 0;
	for (i = begin; i < end; i++) {
		const ll v = GetTaste(i);
		total += v;
		current = min(0LL, current + v);
		min_best = min(min_best, current);
	}

	/* explore until finished */
	for (; current < 0 && i < N; i++) {
		const ll v = GetTaste(i);
		current = min(0LL, current + v);
		min_best = min(min_best, current);
	}

	if (node_id != number_of_nodes - 1) {
		Receive(node_id + 1);
		total += GetLL(node_id + 1);
		min_best = min(min_best, GetLL(node_id + 1));
	}

	if (node_id > 0) {
		PutLL(node_id - 1, total);
		PutLL(node_id - 1, min_best);
		Send(node_id - 1);
	} else {
		printf("%lld\n", total - min_best);
	}


	return 0;
}