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
#include <cstdio>
#include "kanapka.h"
#include "message.h"

inline long long min(long long a, long long b) {
    return a < b ? a : b;
}

inline long long min(long long a, long long b, long long c) {
    return min(min(a, b), c);
}

int main() {
    int nodeId = MyNodeId();
    int nodesCount = NumberOfNodes();
    long long N = GetN();

    long long elems = N / nodesCount;
    if (elems == 0) { 
        elems = 1;
        nodesCount = N;
        if (nodeId >= N) return 0;
    }

    long long start = nodeId * elems, end = start + elems;
    if (nodeId == nodesCount - 1) {
        end = N;
        elems = end - start;
    }

    long long sum = 0, rsum = 0, tmin = 0, cmin = 0, lmin = 0, rmin = 0;
    for (long long i = start; i < end; ++i) {
        sum += GetTaste(i);
        lmin = min(lmin, sum);

        rsum += GetTaste(end - (i - start) - 1);
        rmin = min(rmin, rsum);

        cmin += GetTaste(i);
        if (cmin > 0) cmin = 0;
        else tmin = min(tmin, cmin);
    }

    if (nodeId == 0) {
        for (int i = 1; i < nodesCount; ++i) {
            Receive(i);

            long long Rsum = GetLL(i);
            long long Rtmin = GetLL(i);
            long long Rlmin = GetLL(i);
            long long Rrmin = GetLL(i);

            tmin = min(tmin, Rtmin, rmin + Rlmin);
            lmin = min(lmin, sum + Rlmin);
            rmin = min(Rrmin, Rsum + rmin);
            sum = sum + Rsum;
        }
        printf("%lld", sum - tmin);
    } else {
        PutLL(0, sum);
        PutLL(0, tmin);
        PutLL(0, lmin);
        PutLL(0, rmin);
        Send(0);
    }

    return 0;
}