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

typedef long long LL;
typedef int bool;

#define false 0
#define true 1

#define MAX_COUNT 20000


inline LL sum_per_one_sign(int count) {
	LL sum = 0;
	bool has_positives = false;
	bool has_negatives = false;

	int i;
	for (i = 1; i <= count; ++i) {
		int val = ElementAt(i);
		sum += val;
		if (val < 0) { has_negatives = true; }
		else { has_positives = true; }
	}

	LL res = (has_positives && has_negatives) ? -1 : has_negatives ? 0 : sum;
	return res;
}

inline LL max_sum(int count) {
	if (count > MAX_COUNT) { return -1; }

	LL sum = 0;

	int i, j;
	for (i = 1; i <= count; ++i) {
		LL subsum = 0;
		for (j = i; j <= count; ++j) {
			int val = ElementAt(j);
			subsum += val;
			if (sum < subsum) { sum = subsum; }
		}
	}

	return sum;
}

int main() {
	LL res = 0;
	int count = Size();

	switch (MyNodeId()) {
		case 1:
			PutLL(0, sum_per_one_sign(count));
			Send(0);
			break;
		case 2:
			PutLL(0, max_sum(count));
			Send(0);
			break;
		case 3:
			// just a test, message is ignored
			PutLL(0, -2);
			Send(0);
			break;
		case 0:
			// "Manager"
			Receive(1);
			res = GetLL(1);
			if (res < 0) {
				Receive(2);
				res = GetLL(2);
			}
			printf("%lld\n", res);
			break;
	}
	return 0;
}