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
#include <bits/stdc++.h>
#include "kanapka.h"
#include "message.h"
#define ll long long

int main(){
    ll n = GetN();
    ll myId = MyNodeId();
    ll cntNodes = NumberOfNodes();

    ll step = n/cntNodes;
    if (step < 2)
        step = 2;
    cntNodes = n/step;
    if (myId >= cntNodes) return 0; // i dont want to use this particular one

    // team-work
    ll L = myId * step;
    ll R = (myId+1) * step; // [, )
    if (myId == cntNodes-1) R = n;

    ll minL, minR, maxL, sum, minM;
    minL = minR = maxL = sum = minM = 0;

    for (ll i = L; i < R; i++){
        sum += GetTaste(i);
        minL = std::min(minL, sum);
        maxL = std::max(maxL, sum);
        minM = std::min(minM, sum - maxL);
    }
    minR = sum - maxL;

    if (myId != 0){
        PutLL(0, sum); PutLL(0, minL); PutLL(0, minR); PutLL(0, minM);
        Send(0);
        return 0;
    }
    else{
        for(ll i = 1; i < cntNodes; i++){
            Receive(i);
            ll tSum = GetLL(i); ll tMinL = GetLL(i); ll tMinR = GetLL(i); ll tMinM = GetLL(i);
            minM = std::min(std::min(minM, tMinM), minR + tMinL);
            sum += tSum;
            minR = std::min(tMinR, tSum + minR);
        }
    }
    std::cout << sum - minM << "\n";
}