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

using namespace std;

int main()
{
	int start = (MyNodeId()*Size())/NumberOfNodes();
	int end = (((MyNodeId()+1)*Size())/NumberOfNodes())-1;
	if(end>Size()-1) {end = Size()-1;}

	//cout << "{" << start << " " << end << "}\n";

	long long sum = 0;

	bool sign = false;

	for(int i = start; i<=end; i++)
	{
		if(ElementAt(i+1)<=0) { sign = true; }
		sum += ElementAt(i+1);
	}
	
	if(MyNodeId()>0)
	{
		PutLL(0, sum);
		PutInt(0, (sign?1:0));
		Send(0);
	}
	else
	{
		for(int i = 1; i<NumberOfNodes(); i++)
		{
			Receive(i);
			sum += GetLL(i);
			sign = (GetInt(i)==1?true:false);
		}
	}

	if(MyNodeId() == 0)
	{ 
		if(!sign) cout << sum << endl;
		else cout << 0 << endl;
	}

	return 0;
}