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
// ./rpa_linux/rpa.sh test --source=main.cpp --extra_flags="-Wall,-Wextra" --nodes=3 < zaw/kan0a.in
#include "kanapka.h"
#include "message.h"
#include <cstdio>
#include <algorithm>

//#define DBG(...) fprintf(stderr, __VA_ARGS__)
#define DBG(...)

typedef int I;
typedef long long L;

using std::min;
using std::max;

static void calc(L begin, L end) {
    L sum = 0;
    L best_local = 0;
    L best_left = 0;
    L worst_left = 0;
    L local = 0;
    for (L l=begin; l<end; ++l) {
        L t = GetTaste(l);
        sum += t;
        best_left = min(best_left, sum);
        worst_left = max(worst_left, sum);
        local = min(0ll, local + t);
        best_local = min(best_local, local);
    }
    L best_right = sum - worst_left;
    PutLL(0, sum);
    PutLL(0, best_local);
    PutLL(0, best_left);
    PutLL(0, best_right);
    Send(0);
}

int main() {
    I nID = MyNodeId();
    I nNodes = NumberOfNodes();
    L n = GetN();

    // podział na nody
    L perNode = n / nNodes;
    L begin = nID * perNode;
    L end = (nID + 1 == nNodes) ? n : begin + perNode;
    DBG("split [%lld, %lld>\n", begin, end);

    // obliczenia
    calc(begin, end);

    // agregacja
    if (nID != 0)
        return 0;

    L sum = 0;
    L best = 0;
    L last_right = 0;
    for (I n=0; n<nNodes; ++n) {
        Receive(n);
        L node_sum = GetLL(n);
        L node_best = GetLL(n);
        L node_left = GetLL(n);
        L node_right = GetLL(n);

        sum += node_sum;
        best = min(best, min(node_best, last_right + node_left));
        last_right = min(node_right, last_right + node_sum);
        DBG("nsum %lld; nbest %lld; nleft %lld; nright %lld; best %lld; lastr %lld\n",
                node_sum, node_best, node_left, node_right, best, last_right);
    }
    DBG("sum %lld; best %lld; ret %lld\n", sum, best, sum - best);

    printf("%lld\n", sum - best);
    return 0;
}