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
85
#include "maklib.h"
#include "message.h"
#include <algorithm>
#include <iostream>
using namespace std;

class Mak 
{

public:
	Mak(int num) : left(0), right(0), maks(0), machine_num(num), end_left(false)
	{
		interval_length = (Size() / NumberOfNodes()) + 1;
	}

	void compute()
	{
		long long int temp_sum = 0;
		for (int i = 1 + (machine_num * interval_length); (i <= ((machine_num + 1) * interval_length)) && (i <= Size()); i++) {
			if (temp_sum + ElementAt(i) < 0) {
				end_left = true;
				temp_sum = 0;	
				right = 0;	
			} else {
				temp_sum += ElementAt(i);
				maks = max(temp_sum, maks);	
				if (!end_left) {
					left += ElementAt(i);
				} else {
					right += ElementAt(i); 			
				}		
			}
		}
		
 		if (machine_num > 0) {
			receiveFromPrev();
			sendToNext();
			sendToCentral();
  		} else {
			sendToNext();
			for (int instancja = 1; instancja < NumberOfNodes(); ++instancja) {
				Receive(instancja);
				maks = max(maks, GetLL(instancja));
			}
			cout << maks << endl;
    	}
   }
private:
	long long int left;
	bool end_left;
	long long int right;
	long long int maks;
	int machine_num;
	int interval_length;

	void sendToNext() {
		if (NumberOfNodes() > machine_num + 1) {
			PutLL(machine_num + 1, right);
			Send(machine_num + 1);
		}
	}
	
	void sendToCentral() {
		PutLL(0, maks);
    	Send(0);
	}
	
	void receiveFromPrev() {
		long long int prev;
		Receive(machine_num - 1);
		prev = GetLL(machine_num - 1);	
		
		if (end_left) {
			maks = max(maks, prev + left);
		} else {
			maks += prev;
			right = maks; 		
		}
	}
};

int main() {
	Mak makExecutor(MyNodeId());
	makExecutor.compute();
}