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
95
96
97
98
99
#include "message.h"
#include "kanapka.h"

#include <iostream>

#define ll long long

using namespace std;

int main() {
    int nodes = NumberOfNodes();
    int me = MyNodeId();

    ll len = GetN();
    ll res = 0;

    if(len <= nodes || nodes == 1) {
        ll* s = new ll[nodes+10];
        for(int i=0;i<len;i++) {
            s[i+1] += s[i] + GetTaste(i);
        }
        ll mi = 0;
        for(int i=0;i<len;i++) {
            for(int j=i;j<=len;j++) {
                mi = min(mi, s[j] - s[i]);
            }
        }
        res = s[(int)len] - mi;
    } else {
        ll* from = new ll[nodes];
        ll* to = new ll[nodes];
        ll cur = 0;
        ll step = len / nodes;
        for(int i=0;i<nodes;i++) {
            from[i] = cur;
            if(i == nodes-1) {
                cur = len;
            } else {
                cur += step;
            }
            to[i] = cur;
        }
        int localLen = (int)(to[me] - from[me]);
        ll* s = new ll[localLen+10];
        ll* mx = new ll[localLen+10];
        ll minLeft = 0;
        for(int i=0;i<localLen;i++) {
            s[i+1] = s[i] + GetTaste(from[me]+i);
            mx[i+1] = max(mx[i], s[i+1]);
            minLeft = min(minLeft, s[i+1]);
        }
        ll minRight = 0;
        ll running = 0;
        ll* mxLeft = new ll[localLen+10];
        for(int i=localLen-1;i>=0;i--) {
            running += s[i+1]-s[i];
            minRight = min(minRight, running);
            mxLeft[i] = max(mxLeft[i+1], running);
        }
        ll worst = 1LL << 62;
        for(int i=0;i<=localLen;i++) {
            worst = min(worst, s[localLen] - (mx[i]+mxLeft[i]));
        }
        PutLL(0, s[localLen]);
        PutLL(0, minLeft);
        PutLL(0, minRight);
        PutLL(0, worst);
        Send(0);
        if(me == 0) {
            ll* sums = new ll[nodes];
            ll* s2 = new ll[nodes+10];
            ll* left = new ll[nodes];
            ll* right = new ll[nodes];
            ll mi = 0;
            for(int i=0;i<nodes;i++) {
                Receive(i);
                sums[i] = GetLL(i);
                mi = min(mi, sums[i]);
                s2[i+1] = s2[i] + sums[i];
                left[i] = GetLL(i);
                mi = min(mi, left[i]);
                right[i] = GetLL(i);
                mi = min(mi, right[i]);
                mi = min(mi, GetLL(i));
            }
            for(int i=-1;i<nodes;i++) {
                ll c = i==-1?0:min(0LL,right[i]);
                for(int j=i+1;j<=nodes;j++) {
                    ll d = c + (s2[j]-s2[i+1]) + (j<nodes?min(0LL,left[j]):0);
                    mi = min(mi, d);
                }
            }
            res = s2[nodes] - mi;
        }
    }
    if(me == 0) {
        cout << res << endl;
    }
}