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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
#include "kanapka.h"
#include "message.h"
#include <iostream>

using namespace std;

int main() {
    long long nodes = NumberOfNodes();
    long long nodeid = MyNodeId();
    long long n = GetN();
    long long count = n/nodes;
    if(count * nodes < n) {
        count++;
    }
    long long leftsum=0, leftmax=-1000000001, leftpos=nodeid*count;
    for(long long i=nodeid*count; i<n && i<(nodeid+1)*count; i++) {
        leftsum += GetTaste(i);
        if(leftmax < leftsum) {
            leftmax = leftsum;
            leftpos = i;
        }
    }

    long long tls, tlm, tlp;
    if(nodeid) {
        Receive(nodeid-1);
        tls = GetLL(nodeid-1);
        tlm = GetLL(nodeid-1);
        tlp = GetLL(nodeid-1);
        if(tlm < tls+leftmax) {
            tlm = tls+leftmax;
            tlp = leftpos;
        }
        tls += leftsum;
        PutLL((nodeid+1)%nodes, tls);
        PutLL((nodeid+1)%nodes, tlm);
        PutLL((nodeid+1)%nodes, tlp);
        Send((nodeid+1)%nodes);
    } else {
        PutLL(1, leftsum);
        PutLL(1, leftmax);
        PutLL(1, leftpos);
        Send(1);
        Receive(nodes-1);
        tls = GetLL(nodes-1);
        tlm = GetLL(nodes-1);
        tlp = GetLL(nodes-1);
    }

    if(nodeid) {
        Receive(0);
        tls = GetLL(0);
        tlm = GetLL(0);
        tlp = GetLL(0);
    } else {
        for(long long i=1; i<nodes; i++) {
            PutLL(i, tls);
            PutLL(i, tlm);
            PutLL(i, tlp);
            Send(i);
        }
    }

    if(tls == tlm) {
        tlp = -1;
    }

    long long rightsum=0, rightmax=-1000000001, rightpos=(nodeid+1)*count-1;
    for(long long i=(nodeid+1)*count-1; i>=nodeid*count; i--) {
        if(i<n && i>tlp) {
            rightsum += GetTaste(i);
            if(rightmax < rightsum)
                rightmax = rightsum;
                rightpos = i;
        }
    }

    long long trs, trm, trp;
    if(nodeid) {
        Receive((nodeid+1) % nodes);
        trs = GetLL((nodeid+1) % nodes);
        trm = GetLL((nodeid+1) % nodes);
        trp = GetLL((nodeid+1) % nodes);
        if(trm < trs+rightmax) {
            trm = trs+rightmax;
            trp = rightpos;
        }
        trs += rightsum;
        PutLL(nodeid-1, trs);
        PutLL(nodeid-1, trm);
        PutLL(nodeid-1, trp);
        Send(nodeid-1);
    } else {
        PutLL(nodes-1, 0);
        PutLL(nodes-1, 0);
        PutLL(nodes-1, 0);
        Send(nodes-1);
        Receive(1);
        trs = GetLL(1);
        trm = GetLL(1);
        trp = GetLL(1);
        if(trm < trs+rightmax) {
            trm = trs+rightmax;
            trp = rightpos;
        }
        trs += rightsum;
    }

    if(tlp == -1) {
        if(nodeid) {
            Receive(0);
            trs = GetLL(0);
            trm = GetLL(0);
            trp = GetLL(0);
        } else {
            for(long long i=1; i<nodes; i++) {
                PutLL(i, trs);
                PutLL(i, trm);
                PutLL(i, trp);
                Send(i);
            }
        }

        leftsum=0;
        leftmax=-1000000001;
        leftpos=nodeid*count;
        for(long long i=nodeid*count; i<n && i<(nodeid+1)*count; i++) {
            if(i < trp) {
                leftsum += GetTaste(i);
                if(leftmax < leftsum) {
                    leftmax = leftsum;
                    leftpos = i;
                }
            }
        }

        if(nodeid) {
            Receive(nodeid-1);
            tls = GetLL(nodeid-1);
            tlm = GetLL(nodeid-1);
            tlp = GetLL(nodeid-1);
            if(tlm < tls+leftmax) {
                tlm = tls+leftmax;
                tlp = leftpos;
            }
            tls += leftsum;
            PutLL((nodeid+1)%nodes, tls);
            PutLL((nodeid+1)%nodes, tlm);
            PutLL((nodeid+1)%nodes, tlp);
            Send((nodeid+1)%nodes);
        } else {
            PutLL(1, leftsum);
            PutLL(1, leftmax);
            PutLL(1, leftpos);
            Send(1);
            Receive(nodes-1);
            tls = GetLL(nodes-1);
            tlm = GetLL(nodes-1);
            tlp = GetLL(nodes-1);
        }

    }

    if(nodeid == 0) {
        cout << (tlm < 0 ? 0 : tlm)+(trm < 0 ? 0 : trm) << endl;
    }


    return 0;
}