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
#include <cstdio>
#include <algorithm>
#include "message.h"
#include "kanapka.h"
using namespace std;

typedef long long ll;

/*ll GetN(){}
ll GetTaste(ll i){}
int NumberOfNodes(){}
int MyNodeId(){}
void PutLL(int target, ll value){}
void Send(int target){}
int Receive(int source){}
ll GetLL(int source){}*/

struct Subseq{
    ll p, q, s, m;
    Subseq(ll a=0, ll b=0, ll c=0, ll d=0)
        : p(a), q(b), s(c), m(d) {}
};

Subseq getSubsequence(ll n){
    ll a=(n*MyNodeId())/NumberOfNodes();
    ll b=(n*(MyNodeId()+1))/NumberOfNodes();
    Subseq res;
    for(ll i=a, c=0, d=0;i<b;++i){
        c+=GetTaste(i);
        res.p=min(res.p, c);
        d+=GetTaste(i);
        if(d>0) d=0;
        res.m=min(res.m, d);
    }
    for(ll i=b-1, c=0;i>=a;--i){
        c+=GetTaste(i);
        res.q=min(res.q, c);
        if(i==a) res.s=c;
    }
    return res;
}

Subseq mergeSubsequences(Subseq a, Subseq b){
    return Subseq(min(a.p, a.s+b.p), min(b.q, b.s+a.q), a.s+b.s, min(a.q+b.p, min(a.m, b.m)));
}

int main()
{
    Subseq y, x=getSubsequence(GetN());
    if(!MyNodeId()){
        ll sum=x.s;
        for(ll i=1;i<NumberOfNodes();++i){
            Receive(i);
            y.p=GetLL(i);
            y.q=GetLL(i);
            y.s=GetLL(i);
            y.m=GetLL(i);
            x=mergeSubsequences(x, y);
            sum+=y.s;
        }
        printf("%lld", sum-x.m);
    }else{
        PutLL(0, x.p);
        PutLL(0, x.q);
        PutLL(0, x.s);
        PutLL(0, x.m);
        Send(0);
    }
}