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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#include<iostream>
#include<algorithm>
#include<list>
#include<map>
#include<queue>
#define FOR(i,n) for(int i = 0;i<n;++i)
#define FORI(i,b,n) for(int i = b;i<n;++i)
#define FORD(i,n) for(int i = n;i>=0;--i)
#define ZERO 0.000001
#define qprintf debug && printf
#define min(a,b) ((a)<(b)?(a):(b))
#define max(a,b) ((a)>(b)?(a):(b))
#define ull long long int
#include "message.h"
#include "maklib.h"
using namespace std;
int main(){
	int N = MyNodeId();
	int k=Size();
	int ns = NumberOfNodes();
	int ws = ns-1;

	if(!N){
		int lefts[ws];
		int rights[ws];
		int mxs[ws];
		int sums[ws];
		FORI(i,1,ns){
			Receive(i);
		}
		FORI(i,1,ns){
			Receive(i);
			lefts[i-1]=GetInt(i);
			rights[i-1]=GetInt(i);
			mxs[i-1]=GetInt(i);
			sums[i-1]=GetInt(i);
		}
		int mx=0;
		int tmp=0;
		FOR(i,ws){
			int wl = tmp+lefts[i];
			if(wl)mx=wl;
			if(mxs[i]>mx)mx=mxs[i];

			tmp+=sums[i];
			if(rights[i]>tmp)tmp=rights[i];
		}
		cout<<mx;
		return 0;
	}

	int start = k/ws*N;
	int end;
	if(N==ws){
		end=k;
	}else{
		end=k/ws*(N-1);
	}

	int left=0;
	int tmp=0;
	FORI(i,start,end){
		tmp+=ElementAt(i);
		if(left<tmp)
			left=tmp;
	}
	int right=0;
	tmp=0;
	for(int i=end-1;i>=start;--i){
		tmp+=ElementAt(i);
		if(right<tmp)
			right=tmp;
	}

	int mx=0;
	tmp = 0;
	FORI(i,start,end){
		tmp+=ElementAt(i);
		if(tmp>mx)mx=tmp;
		if(tmp<0)tmp=0;
	}

	int sum=0;
	FORI(i,start,end){
		sum+=ElementAt(i);
	}
	PutInt(0,left);
	PutInt(0,right);
	PutInt(0,mx);
	PutInt(0,sum);
	Send(0);

  return 0;
}