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
#include "message.h"
#include "maklib.h"
#include <iostream>
#include <cstdio>
#include <vector>

using namespace std;

const long long int INF = 1000LL * 1000 * 1000 * 1000 * 1000 * 1000;

int main()
{
	int instance = MyNodeId();
	int nOfNodes = NumberOfNodes();
	int n = Size();
	
	int first = 1 + (instance * n) / nOfNodes;
	int last = ((instance + 1) * n) / nOfNodes;
	int length = last - first + 1;
	
	long long int prefSums[length+1];
	prefSums[0] = 0;
	
	for(int i = 1; i <= length; i++)
	{
		prefSums[i] = prefSums[i-1] + ElementAt(first + i - 1);
	}
	
	for(int i = instance + 1; i < nOfNodes; i++)
	{
		PutLL(i, prefSums[length]);
		Send(i);
	}
	
	long long int prefSumBefore = 0;
	for(int i = 0; i < instance; i++)
	{
		Receive(i);
		prefSumBefore += GetLL(i);
	}
	
	long long int maxValInPref = -INF;
	for(int i = 0; i <= length; i++)
	{
		maxValInPref = max(maxValInPref, prefSumBefore + prefSums[i]);
	}
	
	for(int i = 0; i < instance; i++)
	{
		PutLL(i, maxValInPref);
		Send(i);
	}
	
	long long int maxValInSuf = -INF;
	for(int i = instance + 1; i < nOfNodes; i++)
	{
		Receive(i);
		maxValInSuf = max(maxValInSuf, GetLL(i));
	}
	
	long long int maxResult = 0;
	for(int i = length; i >= 0; i--)
	{
		maxResult = max(maxResult, maxValInSuf - (prefSumBefore + prefSums[i]));
		maxValInSuf = max(maxValInSuf, prefSumBefore + prefSums[i]);
	}
	
	if(instance != 0)
	{
		PutLL(0, maxResult);
		Send(0);
	}
	else
	{
		long long int result = 0;
		for(int i = 1; i < nOfNodes; i++)
		{
			Receive(i);
			result = max(result, GetLL(i));
		}
		
		printf("%lld\n", result);
	}
}