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

#include <cstdio>
#include <algorithm>
using namespace std;

int main()
{
	int len = (Size()-1)/NumberOfNodes() + 1;
	int from = MyNodeId()*len;
	int to = min(from + len, Size());
	long long normalSum = 0, maxNormalSum = 0, subSum = 0, maxSubSum = 0;
	for(int i = from; i < to; ++i)
	{
		normalSum += ElementAt(i+1);
		maxNormalSum = max(maxNormalSum, normalSum);
		subSum += ElementAt(i+1);
		subSum = max(subSum, 0LL);
		maxSubSum = max(maxSubSum, subSum);
	}

	if(MyNodeId() > 0)
	{
		PutLL(0, normalSum);
		PutLL(0, maxNormalSum);
		PutLL(0, subSum);
		PutLL(0, maxSubSum);
		Send(0);
	}
	else
	{
		long long whole, prefix, sufix, inside, result = maxSubSum, inProgress = subSum;
		for(int i = 1; i < NumberOfNodes(); ++i)
		{
			Receive(i);
			whole = GetLL(i);
			prefix = GetLL(i);
			sufix = GetLL(i);
			inside = GetLL(i);

			result = max(result, inside);
			result = max(result, inProgress + prefix);
			inProgress = max(inProgress + whole, sufix);
		}
		result = max(result, inProgress);
		printf("%lld\n", result);
	}

}