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

using namespace std;

typedef long long LL;

const int N = 1e3 + 7;
const LL INF = 1e18 + 7;
LL n, m;
LL MX[N], MN[N], S[N];

int main()
{
    m = n = GetN();
    while(m % 10) m++;
    LL a = MyNodeId() * m / NumberOfNodes();
    LL b = (MyNodeId() + 1) * m / NumberOfNodes();
    LL mn = INF, mx = -INF, s = 0, res = INF;
    for(LL i = a; i < b; i++)
    {
        if(i >= n) continue;
        LL v = GetTaste(i);
        s += v;
        res = min(res, s - mx);
        mx = max(mx, s);
        mn = min(mn, s);
    }
    if(MyNodeId() != 0)
    {
        PutLL(0, mx);
        PutLL(0, mn);
        PutLL(0, s);
        PutLL(0, res);
        Send(0);
    }
    else
    {
        MX[0] = mx, MN[0] = mn, S[0] = s;
        for(int i = 1; i < NumberOfNodes(); i++)
        {
            int r = Receive(i);
            MX[i] = S[i - 1] + GetLL(r);
            MN[i] = S[i - 1] + GetLL(r);
            S[i] = S[i - 1] + GetLL(r);
            res = min(res, GetLL(r));
            res = min(res, MN[i] - mx);
            mx = max(mx, MX[i]);
        }
        cout << S[NumberOfNodes() - 1] - res;
    }
    return 0;
}