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
#include "kanapka.h"
#include "message.h"
#include <cstdio>
#include <iostream>
#include <algorithm>
#define SSS 5000000LL

using namespace std;
long long tot[SSS],suf[SSS],pref[SSS];
long long mpref[SSS],msuf[SSS];
int main() {
    int nodes = NumberOfNodes();
    int myId = MyNodeId();
    int N = GetN();

    int st = (N/nodes) * myId;
    int en = min(N-1,((N/nodes) * (myId+1)) - 1);

    long long maxpref = 0;
    long long curpref = 0;
    long long cursuf = 0;

    long long maxsuf = 0;
    long long totsuf = 0;
    long long total = 0;

    for(int i = st; i <= en; i++) {
        total += GetTaste(i);
        curpref += GetTaste(i);
        maxpref = max(maxpref,curpref);
    }

    for(int i = en; i >= st; i--) {
        cursuf += GetTaste(i);
        maxsuf = max(maxsuf,cursuf);
    }

    //kolejnosc: total, maxpref, maxsuf
    PutLL(0,total);
    PutLL(0,maxpref);
    PutLL(0,maxsuf);
    Send(0);


    if(myId==0) {
        for(int i = 0; i < nodes; i++) {
            Receive(i);
            tot[i] = GetLL(i);
            pref[i] = GetLL(i);
            suf[i] = GetLL(i);
        }
        long long toto = 0;
        long long to = 0;
        mpref[0] = max(0LL,pref[0]);
        for(int i = 1; i < nodes; i++) {
            mpref[i] = max(mpref[i-1],to+pref[i]);
            to += tot[i];
        }
        toto = to;
        to = 0;

        for(int i = nodes-1; i >= 0; i--) {
            msuf[i] = max(msuf[i+1],to+suf[i]);
            to += tot[i];
        }
        long long res = 0;
        for(int i = 0; i < nodes; i++) {
            res = max(mpref[i]+msuf[i],res);
        }
        if(res > toto) res = toto;
        cout << res << "\n";
    }


    return 0;
}