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
77
78
79
80
#include "kanapka.h"
#include "message.h"

#include <iostream>
#include <vector>

using namespace std;

static const long long N = GetN();
static int MASTER = 0;
static int BEGIN = 0;
static int END = 0;

void Sum(bool from_begin) {
    long long sum = 0;
    long long maximum = 0;
    int step = (from_begin) ? 1 : -1;
    int b = (from_begin) ? 0 : N - 1;
    int e = (from_begin) ? N : -1;
    cerr << "b = " << b << "e = " << e << " step = " << step << "\n";
    for (int i = b; i != e; i += step) {
        sum += GetTaste(i);
        maximum = max(sum, maximum);
        PutLL(MASTER, maximum);
        cerr << i << "\n";
    }
    Send(MASTER);
}

void SumFromBegin()
{
    Sum(true);
}

void SumFromEnd()
{
    Sum(false);                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 (false);
}

long long Combine() {
    long long maximum = 0;

    vector<long long> sums(N + 1, 0);

    Receive(BEGIN);
    for (int i = 1; i <= N; ++i) {
        sums[i] += GetLL(BEGIN);
    }
    Receive(END);
    for (int i = N-1; i >= 0; --i) {
        sums[i] += GetLL(END);
        maximum = max(maximum, sums[i]);
    }

    return maximum;
}

void Setup() {
    if (NumberOfNodes() > 1)
        BEGIN = 1;
    if (NumberOfNodes() > 2)
        END = 2;
}

int main() {
    Setup();

if (MyNodeId() == BEGIN) {
        SumFromBegin();
    }
if (MyNodeId() == END) {
        SumFromEnd();
    }
if (MyNodeId() == MASTER) {
    long long result = Combine();

    cout << result;
}
cerr << "end " << MyNodeId() << "\n";
}