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
#include "maklib.h"
#include "message.h"
#include <cstdio>
#include <algorithm>
typedef long long int LL;

int main() {
    int start = ((LL)MyNodeId()*Size())/NumberOfNodes();
    int end = ((LL)(MyNodeId()+1)*Size())/NumberOfNodes();

    //printf("robie %d %d\n", start, end-1);

    LL pref = 0, bestPref = 0;
    for (int i=start; i<end; i++) {
        pref += ElementAt(i+1);
        bestPref = std::max(bestPref, pref);
    }
    LL suf = 0, bestSuf = 0;
    for (int i=end-1; i>=start; i--) {
        suf += ElementAt(i+1);
        bestSuf = std::max(bestSuf, suf);
    }
    LL best = 0, current = 0;
    for (int i=start; i<end; i++) {
        current += ElementAt(i+1);
        current = std::max(current, 0LL);
        best = std::max(best, current);
    }

    PutLL(0, bestPref);
    PutLL(0, bestSuf);
    PutLL(0, pref);
    PutLL(0, best);
    Send(0);
    
    if (MyNodeId() == 0) {
        LL suf = 0, result = 0;
        for (int i=0; i<NumberOfNodes(); i++) {
            Receive(i);
            LL ipref = GetLL(i);
            LL isuf = GetLL(i);
            LL isum = GetLL(i);
            LL ibest = GetLL(i);

            result = std::max(result, ibest);
            result = std::max(result, ipref+suf);

            suf = std::max(isuf, isum+suf);
        };

        printf("%Ld\n", suf);
    }
}