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

using namespace std;

long long preProcess()
{
	long long maxi = 0;
	for (long long i = 0 + MyNodeId(); i < Size(); i++)
	{
		maxi += ElementAt(i + 1);
	}
	return maxi;
}

int main()
{
	long long sum = 0;
	long long maxi = preProcess();
	long long nodes = NumberOfNodes();
	
	for (long long i = nodes + MyNodeId(); i < Size(); i += nodes) {

		sum = 0;
		//cout << "Node #" << MyNodeId() << ": ";

		for (long long j = i; j < Size(); j++) 
		{
			sum += ElementAt(j+1);
			//cout << ElementAt(j + 1) << ", ";
		}
		maxi = max(maxi, sum);
		
		//cout << " = " << sum << "\n";
	}
	//cout << "MAXI = " << maxi << endl;
	

	//For nodes higher than 0 compute highest subarray.
	if (MyNodeId() > 0) 
	{
		PutLL(0, maxi);
		Send(0);
	} 
	else //For first node (0) collect all sums and choose highest one.
	{

		for (long long inst = 1; inst < nodes; inst++)
		{	
			Receive(inst);
			maxi = max(maxi, GetLL(inst)); 
		}
		cout << maxi << endl;
	}	
	
	return 0;
}