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
#include <iostream>
#include <vector>

#include "message.h"
#include "kanapka.h"

struct sub_res {
    int best;
    int left;
    int right;
};
const sub_res max_subarray(const std::vector<long long int> &ar) {
    int left = 0;
    int current_left = 0;
    int right = 0;
    int max = 0;
    int acc = 0;
    for (long long el : ar) {
        if (el > 0 && acc == 0)
            current_left = left;
        left += el;
        acc += el;
        if (acc > max) {
            max = acc;
            right = 0;
        } else
            right += el;

        if (acc < 0)
            acc = 0;
    }
    return {max, current_left, right};
}


int main() {

    int N = NumberOfNodes();
    int node = MyNodeId();

    long long int n = GetN();
    long long lenPerNode = n / N;
    long long start = lenPerNode * node;
    long long end = (node == N - 1) ? n : lenPerNode * (node + 1);

    long long int size = end - start;
    std::vector<long long int> data((unsigned long) size);
    for (int i = 0; i < size; i++)
        data[i] = -GetTaste(start + i);

    const sub_res &res = max_subarray(data);
    if (node != 0) {
        PutLL(0, res.left);
        PutLL(0, res.best);
        PutLL(0, res.right);
        Send(0);
    } else {
        std::vector<long long int> all((unsigned long long)(N * 3));
        all[0] = res.left;
        all[1] = res.best;
        all[2] = res.right;
        for (int i = 1; i < N; i++) {
            const int src = Receive(-1);
            all[src * 3] = GetLL(src);
            all[src * 3 + 1] = GetLL(src);
            all[src * 3 + 2] = GetLL(src);
        }
        const sub_res &all_res = max_subarray(all);
        std::cout << (-all_res.left + -all_res.right) << std::endl;
    }

    return 0;
}