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

int main()
{
	long long e = ElementAt(1);
 	long long id = MyNodeId();
	long long n = NumberOfNodes();
	long long s = Size();

	if (e < 0) {
		if (!id) {
			printf("0\n");
		}
	} else {
		long long a = (id * s) / n;
		long long b = ((id + 1) * s) / n;
		long long sum = 0;
		for (int i = a + 1; i < b + 1; i++) {
			sum += ElementAt(i);
		}
		if (id) {
			PutLL(0, sum);
			Send(0);
		} else {
			for (int i = 1; i < n; i++) {
				Receive(i);
				sum += GetLL(i);
			}
			printf("%lld\n", sum);
		}
	}
}