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
// Karol Kosinski
#include "message.h"
#include "maklib.h"
#include <cstdio>
#include <algorithm>
#define FOR(i,a,b) for(int i=(a); i<(b); ++i)
using namespace std;

typedef long long LL;

LL sum(int left, int right) {
        LL s = 0LL;
        FOR(i,left,right) s += ElementAt(i+1);
        return s;
}

LL max_sum(int left, int right) {
        LL s = 0LL, res = 0LL;
        FOR(i,left,right) {
                s += ElementAt(i+1);
                if(s < 0) s = 0LL;
                res = max(res, s);
        }
        return res;
}

LL max_left_sum(int left, int right) {
        LL s = 0LL, res = 0LL;
        FOR(i,left,right) {
                s += ElementAt(i+1);
                res = max(res, s);
        }
        return res;
}

LL max_right_sum(int left, int right) {
        LL s = 0LL, res = 0LL;
        FOR(i,left,right) {
                s += ElementAt(left+right-i);
                res = max(res, s);
        }
        return res;
}

int main() {
        int non = NumberOfNodes(), id = MyNodeId(), n = Size();
        if(id < n) {
                non = min(non, n);
                int left = int(LL(id)*n/non), right = int(LL(id+1)*n/non);
                LL SL[] = { sum           (left, right),
                            max_sum       (left, right),
                            max_left_sum  (left, right),
                            max_right_sum (left, right) };
                if(id > 0) {
                        FOR(i,0,4) PutLL(0, SL[i]);
                        Send(0);
                }
                else {
                        FOR(inst,1,non) {
                                Receive(inst);
                                LL S[4], SR[4];
                                FOR(i,0,4) SR[i] = GetLL(inst);
                                S[0] = SL[0] + SR[0];
                                S[1] = max(max(SL[1], SR[1]), SL[3] + SR[2]);
                                S[2] = max(SL[2], SL[0] + SR[2]);
                                S[3] = max(SL[3] + SR[0], SR[3]);
                                FOR(i,0,4) SL[i] = S[i];
                        }
                        printf("%lld\n", SL[1]);
                }
        }
        return 0;
}