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

typedef long long int64;

typedef struct {
	int64 sum;
	int64 max;
	int64 maxs;
	int64 maxe;
} result;

int actual_size = Size();

inline int safe_element(int n) {
	if (n < 0 || n >= actual_size) {
		return 0;
	}
	return ElementAt(n + 1);
}

int id = MyNodeId();
int nodes = NumberOfNodes();
int segment_size = ceil((double)actual_size / nodes);
int start = segment_size * id;
int end = segment_size * (id + 1);

int main() {
	int64 my_sum = 0;
	int64 my_max = 0;
	int64 my_maxs = 0;
	int64 my_maxe = 0;
	for (int i = start; i < end; ++i) {
		int el = safe_element(i);
		my_sum += el;
		if (my_sum > my_maxs) {
			my_maxs = my_sum;
		}
		if (my_maxe > 0) {
			my_maxe += el;
		}
		else {
			my_maxe = el;
		}
		if (my_maxe > my_max) {
			my_max = my_maxe;
		}
	}
	if (id) {
		PutLL(0, my_sum);
		PutLL(0, my_max);
		PutLL(0, my_maxs);
		PutLL(0, my_maxe);
		Send(0);
		return 0;
	}
	for (int i = 1; i < nodes; ++i) {
		Receive(i);
		int64 o_sum = GetLL(i);
		int64 o_max = GetLL(i);
		int64 o_maxs = GetLL(i);
		int64 o_maxe = GetLL(i);
		int64 new_sum = my_sum + o_sum;
		int64 new_maxs = std::max(my_maxs, my_sum + o_maxs);
		int64 new_maxe = std::max(o_maxe, o_sum + my_maxe);
		int64 new_max = std::max(my_max, std::max(o_max, my_maxe + o_maxs));
		my_sum = new_sum;
		my_max = new_max;
		my_maxs = new_maxs;
		my_maxe = new_maxe;
	}
	printf("%lld\n", my_max);
	return 0;
}