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

int partSize, startPoint;
long long maxSum, maxBegin, maxEnd, sum, nonNegativeSum, tmp;

int main()
{
	maxSum = maxBegin = maxEnd = nonNegativeSum = 0;

	partSize = Size()/NumberOfNodes();
	startPoint = MyNodeId() * partSize;

	if(MyNodeId() == NumberOfNodes()-1) partSize += Size() % NumberOfNodes();
	
	for(int i = 0; i < partSize; ++i)
	{
		tmp = ElementAt(startPoint+i+1);
		sum += tmp;
		nonNegativeSum += tmp;
		nonNegativeSum = std::max(nonNegativeSum,0LL);
		maxBegin = std::max(maxBegin,sum);
		maxSum = std::max(maxSum,nonNegativeSum);
	}

	sum = 0;
	for(int i = partSize-1; i >= 0; --i)
	{
		tmp = ElementAt(startPoint+i+1);
		sum += tmp;
		maxEnd = std::max(maxEnd,sum);
	}

	if(MyNodeId() > 0)
	{
		PutLL(0,maxSum);
		PutLL(0,maxBegin);
		PutLL(0,maxEnd);
		PutLL(0,sum);
		Send(0);
	}
	else
	{
		long long finalResult = maxSum, asdf = maxEnd;
		long long maxSumO, maxBeginO, maxEndO, sumO;
		for(int i = 1; i < NumberOfNodes(); ++i)
		{
			Receive(i);
			maxSumO = GetLL(i);
			maxBeginO = GetLL(i);
			maxEndO = GetLL(i);
			sumO = GetLL(i);
			finalResult = std::max(finalResult,maxSumO);
			finalResult = std::max(finalResult,asdf+maxBeginO);
			asdf = std::max(asdf+sumO,maxEndO);
		}

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