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

using namespace std;

#define FOR(v,p,k) for(int v=p;v<=k;++v)
#define FORD(v,p,k) for(int v=p;v>=k;--v)
#define REP(i,n) for(int i=0;i<(n);++i)
#define ALL(c) (c).begin(),(c).end()
#define ZERO(x) memset(x,0,sizeof(x))

typedef long long LL;
typedef unsigned long long ULL;

const int INF = 1000000000;

int main(int argc, char **args)
{
	int N = Size();
	int numberOfNodes = min(NumberOfNodes(), N);
	int myNodeId = MyNodeId();

	// Unused node
	if (myNodeId >= numberOfNodes)
		return 0;	

	// Calculate node range
	int begin = (myNodeId * N) / numberOfNodes;
	int end = ((myNodeId + 1) * N) / numberOfNodes;

	// Calculate
	LL best, left, whole, right;
	best = left = whole = right = ElementAt(begin + 1);

	FOR(i, begin + 1, end - 1)
	{
		LL nextElement = ElementAt(i + 1);
		whole += nextElement;
		left = max(left, whole);
		right = max(right + nextElement, nextElement);
		best = max(best, right);
	}

	// Merge
	if (myNodeId > 0)
	{
		PutLL(0, best);
		PutLL(0, left);
		PutLL(0, whole);
		PutLL(0, right);
		Send(0);
	}
	else
	{
		best = max(best, 0LL);

		FOR(i, 1, numberOfNodes - 1)
		{
			Receive(i);
			LL nBest = GetLL(i);
			LL nLeft = GetLL(i);
			LL nWhole = GetLL(i);
			LL nRight = GetLL(i);
			best = max(best, nBest);
			best = max(best, right + nLeft);
			right = max(right + nWhole, nRight);
		}

		printf("%lld\n", best);
	}

	return 0;
}