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
#include <bits/stdc++.h>
#include "message.h"
#include "maklib.h"

#define FWD(a,b,c) for(int a=(b); a<(c); ++a)

typedef long long LL;

using namespace std;

struct result {
	LL pref, suf, inf, sum;
};

inline result operator+(const result &a, const result &b){
	return result({
		max(a.pref, a.sum + b.pref),
		max(a.suf + b.sum, b.suf),
		max(a.suf + b.pref, max(a.inf, b.inf)),
		a.sum + b.sum
	});
}

inline result make(int x){
	return result({
		max(0,x),
		max(0,x),
		max(0,x),
		x
	});
}

int main(){
	int u = MyNodeId();
	int us = NumberOfNodes();
	int n = Size();
	int p = ((LL)u * n) / us + 1;
	int k = (((LL)u + 1) * n) / us + 1;
	result r = make(0);
	FWD(i,p,k)
		r = r + make(ElementAt(i));
	int w = 1;
	while(w < us){
		if(u & w){
			PutLL(u-w, r.pref);
			PutLL(u-w, r.suf);
			PutLL(u-w, r.inf);
			PutLL(u-w, r.sum);
			Send(u-w);
			return 0;
		}else if(u + w < us){
			Receive(u+w);
			result q;
			q.pref = GetLL(u+w);
			q.suf = GetLL(u+w);
			q.inf = GetLL(u+w);
			q.sum = GetLL(u+w);
			r = r + q;
		}
		w *= 2;
	}
	printf("%lld\n", r.inf);
	return 0;
}