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
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
#include "message.h"
#include "kanapka.h"
#include <cstdio>
#include <algorithm>

typedef long long LL;

int main() {
    if (NumberOfNodes() < 2) {
        LL total = 0, maxLeft = 0;
        int whereLeft = -1;
        for (int i = 0; i < (int)GetN(); ++i) {
            total += GetTaste((LL)i);
            if (maxLeft < total) {
                maxLeft = total;
                whereLeft = i;
            }
        }
        total = 0;
        LL maxRight = 0;
        int whereRight = (int)GetN();
        for (int i = (int)GetN() - 1; i > -1; --i) {
            total += GetTaste((LL)i);
            if (maxRight < total) {
                maxRight = total;
                whereRight = i;
            }
        }
        if (whereLeft < whereRight)
            printf("%lld\n", maxLeft + maxRight);
        else
            printf("%lld\n", std::max(maxLeft, maxRight));
    }
    else {
        if (MyNodeId()) {
            Receive(0);
            if (GetChar(0)) {
                LL total = 0, maxLeft = 0;
                int from = GetInt(0), to = GetInt(0), whereLeft = from - 1;
                for (int i = from; i < to; ++i) {
                    total += GetTaste((LL)i);
                    if (maxLeft < total) {
                        maxLeft = total;
                        whereLeft = i;
                    }
                }
                PutLL(0, maxLeft);
                PutInt(0, whereLeft);
                PutLL(0, total);
                PutInt(0, to - 1);
                Send(0);
            }
            else {
                LL total = 0, maxRight = 0;
                int from = GetInt(0), to = GetInt(0), whereRight = from + 1;
                for (int i = from; i > to; --i) {
                    total += GetTaste((LL)i);
                    if (maxRight < total) {
                        maxRight = total;
                        whereRight = i;
                    }
                }
                PutLL(0, maxRight);
                PutInt(0, whereRight);
                PutLL(0, total);
                PutInt(0, to + 1);
                Send(0);
            }
        }
        else {
            int blockSize = GetN() / (NumberOfNodes() - 1), from = 0;
            for (int i = 1; i < NumberOfNodes() - 1; ++i) {
                PutChar(i, 1);
                PutInt(i, from);
                from += blockSize;
                PutInt(i, from);
                Send(i);
            }
            PutChar(NumberOfNodes() - 1, 1);
            PutInt(NumberOfNodes() - 1, from);
            PutInt(NumberOfNodes() - 1, (int)GetN());
            Send(NumberOfNodes() - 1);
            LL total = 0, extra, maxLeft = 0;
            int whereLeft = -1, trash;
            for (int i = 1; i < NumberOfNodes(); ++i) {
                Receive(i);
                extra = GetLL(i);
                total += extra;
                if (maxLeft < total) {
                    maxLeft = total;
                    whereLeft = GetInt(i);
                }
                else
                    trash = GetInt(i);
                total += -extra + GetLL(i);
                if (maxLeft < total) {
                    maxLeft = total;
                    whereLeft = GetInt(i);
                }
                else
                    trash = GetInt(i);
            }
            from = (int)GetN() - 1;
            for (int i = 1; i < NumberOfNodes() - 1; ++i) {
                PutChar(i, 0);
                PutInt(i, from);
                from -= blockSize;
                PutInt(i, from);
                Send(i);
            }
            PutChar(NumberOfNodes() - 1, 0);
            PutInt(NumberOfNodes() - 1, from);
            PutInt(NumberOfNodes() - 1, 0);
            Send(NumberOfNodes() - 1);
            LL maxRight = 0;
            int whereRight = (int)GetN();
            for (int i = 1; i < NumberOfNodes(); ++i) {
                Receive(i);
                extra = GetLL(i);
                total += extra;
                if (maxRight < total) {
                    maxRight = total;
                    whereRight = GetInt(i);
                }
                else
                    trash = GetInt(i);
                total += -extra + GetLL(i);
                if (maxRight < total) {
                    maxRight = total;
                    whereRight = GetInt(i);
                }
                else
                    trash = GetInt(i);
            }
            if (whereLeft < whereRight)
                printf("%lld\n", maxLeft + maxRight);
            else
                printf("%lld\n", std::max(maxLeft, maxRight));
        }
    }
    return 0;
}