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
#include <bits/stdc++.h>
#include "kanapka.h"
#include "message.h"
using namespace std;
using ll  = long long;
using Vi  = vector<int>;
using Pii = pair<int,int>;
#define x first
#define y second
#define pb push_back
#define mp make_pair
#define rep(i,b,e) for(int i=(b); i<(e); i++)
#define each(a,x)  for(auto& a : (x))
#define all(x)     (x).begin(),(x).end()
#define sz(x)      int((x).size())
#define endl "\n"
const long long inf = 2e17 + 10;

void prepareSegments( long long n, long long procId, long long numberOfProc ){
	long long beg = n*(procId-1)/(numberOfProc-1);
	long long end = n*(procId)/(numberOfProc-1);
	long long pref = 0, maxPref = 0;
	for( int i = beg; i < end; i++ ){
		long long x = GetTaste(i);
		pref += x;
		maxPref = max(maxPref,pref);
	}
	PutLL(0,pref);
	PutLL(0,maxPref);
	Send(0);
}

void count( long long n, long long procId, long long numberOfProc ){
	Receive(0);
	long long pref = GetLL(0);
	long long maxPref = GetLL(0);
	long long beg = n*(procId-1)/(numberOfProc-1);
	long long end = n*(procId)/(numberOfProc-1);
	long long res = inf;
	for( int i = beg; i < end; i++ ){
		long long x = GetTaste(i);
		pref += x;
		maxPref = max(maxPref,pref);
		res = min( res, pref - maxPref );
	}
	PutLL(0,res);
	Send(0);
}


long long countPrepare( int n, int procId, int numberOfProc ){
	long long pref=0, maxPref=0;
	for( int i = 1; i < numberOfProc; i++ ){
		PutLL(i,pref);
		PutLL(i,maxPref);
		Send(i);
		Receive(i);
		long long sum = GetLL(i);
		long long maxSum = GetLL(i);
		maxPref = max( maxPref, pref + maxSum );
		pref += sum;
	}
	return pref;
}

int main() {
	int numberOfProc = NumberOfNodes(); 
	int procId = MyNodeId();
	int n = GetN();
	long long res = 0;
	if( procId != 0 ){
		prepareSegments(n,procId,numberOfProc);
		count(n,procId,numberOfProc);
	}else {
		long long sum = countPrepare(n,procId,numberOfProc);
		for( int i = 1; i < numberOfProc; i++ ){
			Receive(i);
			long long kan = GetLL(i);
			res = max( res, sum - kan );
		}
		cout << res << endl;
	}
}