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
#include<bits/stdc++.h>
#include"kanapka.h"
#include"message.h"
using namespace std;
#ifndef d
#define d(...)
#endif
#define st first
#define nd second
#define pb push_back
#define siz(c) (int)(c).size()
#define all(c) (c).begin(), (c).end()
typedef long long LL;
typedef long double LD;
constexpr int INF=1e9+7;
constexpr LL INFL=1e18;
template<class L, class R> ostream &operator<<(ostream &os, pair<L,R> P) {
  return os << "(" << P.st << "," << P.nd << ")";
}

struct socket
{
    uint32_t conn;
    socket(uint32_t _conn) : conn(_conn) {}
    socket& operator+ () { Receive(conn); return *this; }
    socket& operator- () { Send(conn); return *this; }
    #define BYTES(_b) template<typename T> typename enable_if<(_b) == sizeof(T), socket&>::type
    BYTES(1) operator<< (T val) { PutChar(conn, val); return *this; }
    BYTES(1) operator>> (T& var) { var = GetChar(conn); return *this; }
    BYTES(4) operator<< (T val) { PutInt(conn, val); return *this; }
    BYTES(4) operator>> (T& var) { var = GetInt(conn); return *this; }
    BYTES(8) operator<< (T val) { PutLL(conn, val); return *this; }
    BYTES(8) operator>> (T& var) { var = GetLL(conn); return *this; }
    #undef BYTES
};

int main()
{
	ios_base::sync_with_stdio(0);
	cin.tie(0);
		
	int num=NumberOfNodes(),me=MyNodeId();
	int n=GetN();
	int len=n/num+1;
	LL best=0, S=0, cur=0;
	LL prfmn=0,prfmx=0;
	for(int i=me*len;i<min(n,me*len+len);i++) {
		LL x=GetTaste(i);
		S+=x;
		prfmn=min(prfmn,S),prfmx=max(prfmx,S);
		cur=min(x,cur+x);
		best=min(best,cur);
	}
	//cerr<<"["<<me*len<<", "<<min(n,me*len+len)<<") : "<<prfmn<<" "<<S-prfmx<<" "<<S<<" "<<best<<endl;
	-(socket(0)<<prfmn<<S-prfmx<<S<<best);
	if(me == 0) {
		best=0,S=0;
		LL l=0,r=0;
		for(int i=0;i<num;i++) {
			LL l1,r1,s,b;
			(+socket(i))>>l1>>r1>>s>>b;
			best=min(best,r+l1);
			best=min(best,b);
			l=min(l,S+l1), r=min(r1,s+r);
			S+=s;
		}
		cout<<S-best<<"\n";
	}
	
	return 0;
}