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

#include <algorithm>
#include <iostream>

int main() {
    long long int N = Size();
    long long int poczatek = (MyNodeId() * N) / NumberOfNodes();
    long long int koniec = ((MyNodeId() + 1) * N) / NumberOfNodes();
    
    long long int min_sum = 0;
    long long int max_sum = 0;
    long long int max_res = 0;
    long long int sum = 0;
    for (long long i = poczatek+1; i < koniec+1; ++i) {
        sum += ElementAt(i);
        if (sum>max_sum) max_sum = sum;
        if (sum<min_sum) min_sum = sum;
        if (sum-min_sum>max_res) max_res = sum - min_sum;
    }

    if (MyNodeId() > 0) {
        PutLL(0, min_sum);
        PutLL(0, max_sum);
        PutLL(0, max_res);
        PutLL(0, sum);
        Send(0);
    } else {
        long long int final_res = max_res;
        for (int instance = 1; instance < NumberOfNodes(); ++instance) {
            Receive(instance);
            long long instance_min_sum = GetLL(instance);
            long long instance_max_sum = GetLL(instance);
            long long instance_max_res = GetLL(instance);
            long long instance_sum = GetLL(instance);

            final_res = std::max(max_res, std::max(instance_max_res, instance_max_sum + sum - min_sum));
            min_sum = std::min(min_sum,sum+instance_min_sum);
            sum = sum+instance_sum;
        }

        std::cout << final_res << std::endl;
    }
    
    return 0;
}