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
#include <cstdio>
#include <cstdlib>
#include <vector>
#include <message.h>

using namespace std;

#include "kanapka.h"

#define LARGE_PRIME 1000000007
#define MASTER_NODE 0

long long cutPoint(long long i, long long N, long long M) {
    return i*(N/M)+min(i, N%M);
}


int main() {
    long long first=cutPoint(MyNodeId(), GetN(), NumberOfNodes());
    long long last=cutPoint(MyNodeId()+1, GetN(), NumberOfNodes());
    long long lPref=0;
    long long lSuf=0;
    long long lInf=0;
    long long lSum=0;
    for (long long i=first; i<last; i++) {
        long long x=GetTaste(i);
        lSuf+=x;
        lSuf=min(lSuf, 0LL);
        lInf=min(lInf, lSuf);
        lSum+=x;
        lPref=min(lPref, lSum);
    }
    PutLL(MASTER_NODE, lPref);
    PutLL(MASTER_NODE, lSuf);
    PutLL(MASTER_NODE, lInf);
    PutLL(MASTER_NODE, lSum);
    Send(MASTER_NODE);
    
    if (MyNodeId()==MASTER_NODE) {
        long long gPref=0;
        long long gSuf=0;
        long long gInf=0;
        long long gSum=0;
        for (int i=0; i<NumberOfNodes(); i++) {
            Receive(i);
            lPref=GetLL(i);
            lSuf=GetLL(i);
            lInf=GetLL(i);
            lSum=GetLL(i);
            
            gInf=min(min(gInf, lInf), gSuf+lPref);

            gPref=min(gPref, gSum+lPref);
            gSuf=min(gSuf+lSum, lSuf);
            
            gSum+=lSum;
        }
        printf("%lld", gSum-gInf);
        
    }
    
    return 0;
}