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
#include "message.h"
#include "kanapka.h"

#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <cmath>
#include <cassert>
#include <cstring>
#include <ext/numeric>
using namespace std ;
using namespace __gnu_cxx ;
typedef long long LL ;
typedef pair<int,int> PII ;
typedef vector<int> VI ;
const int INF = 1e9+100 ;
const LL INFLL = (LL)INF * (LL)INF ;
#define REP(i,n) for(i=0;i<(n);++i)
#define ALL(c) c.begin(),c.end()
#define VAR(v,i) __typeof(i) v=(i)
#define FOREACH(i,c) for(VAR(i,(c).begin());i!=(c).end();++i)
#define CLEAR(t) memset(t,0,sizeof(t))
#define PB push_back
#define MP make_pair
#define FI first
#define SE second

template<class T1,class T2> ostream& operator<<(ostream &s, const pair<T1,T2> &x) { s<< "(" << x.FI << "," << x.SE << ")"; return s;}
template<class T>           ostream& operator<<(ostream &s, const vector<T>   &t) { FOREACH(it, t) s << *it << " " ; return s ; }
template<class T>           ostream& operator<<(ostream &s, const set<T>      &t) { FOREACH(it, t) s << *it << " " ; return s ; }
template<class T1,class T2> ostream& operator<<(ostream &s, const map<T1, T2> &t) { FOREACH(it, t) s << *it << " " ; return s ; }

///////////////////////////////////////////

void join(LL &s, LL &d, LL &pref, LL &suf,
          LL s1, LL d1, LL pref1, LL suf1,
          LL s2, LL d2, LL pref2, LL suf2) {
	s = s1+s2 ;
	d = min(min(d1,d2), suf1+pref2) ;
	pref = min(pref1, s1+pref2) ;
	suf = min(suf2, s2+suf1) ;
}

int main()
{
	ios_base::sync_with_stdio(0) ;
	LL start =    MyNodeId() *GetN()/NumberOfNodes() ;
	LL end   = (MyNodeId()+1)*GetN()/NumberOfNodes() ;
	
	LL s=0, d=0, pref=0, act=0 ;
	for(LL i=start ; i<end ; i++) {
		LL v = GetTaste(i) ;
		s += v ;
		act = min(act+v, 0LL)  ;
		d = min(d, act) ;
		pref = min(pref, s) ;
	}
	LL suf=0, tmp=0 ;
	for(LL i=end-1 ; i>=start ; i--) {
		tmp += GetTaste(i) ;
		suf = min(suf, tmp) ;
	}
	
	if(MyNodeId()>0) {
		PutLL(0, s) ;
		PutLL(0, d) ;
		PutLL(0, pref) ;
		PutLL(0, suf) ;
		Send(0) ;
	}
	else {
		LL s2, d2, pref2, suf2 ;
		for(int i=1 ; i<NumberOfNodes() ; i++) {
			Receive(i) ;
			s2 = GetLL(i) ;
			d2 = GetLL(i) ;
			pref2 = GetLL(i) ;
			suf2 = GetLL(i) ;
			join(s,d,pref,suf, s,d,pref,suf, s2,d2,pref2,suf2) ;
		}
		cout << s-d << endl ;
	}
}