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
#include <iostream>
#include <cstdio>
#include <string>
#include <sstream> 
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <cmath>
#include <algorithm>
#include <cstring>
#include <ctime>
#include <cassert>

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

using namespace std;
#define pb push_back
#define mp make_pair
#define pii pair<int,int>
#define fi first
#define se second
#define vi vector<int>
#define vpii vector<pii>
#define SZ(x) ((int)(x.size()))
#define DBG cerr << "debug here" << endl;
#define DBGV(vari) cerr << #vari<< " = "<< (vari) <<endl;

typedef long long ll;
const int INF = 1e9;

const int LEN = 5e6+10;
ll a[LEN];
ll pref[LEN];
ll suf[LEN];

const int MASTER = 0;

int main()
{
    ios_base::sync_with_stdio(0);

    const ll id = MyNodeId();
    const ll n = GetN();
    const ll m = min(n, (ll)NumberOfNodes());

    //don't need such instances
    if (id >= m) return 0;

    const ll len = ceil(n / (double)m);
    const ll A = id*len;
    const ll B = min(n-1, (id+1)*len-1);
    const ll mylen = B-A+1;

    cerr << "! " << A << " " << B << " " << n << endl;


    if (false and n <= 1e5) {
        // instance 0 solves locally
        if (id == 0) {
            for (int i = 0; i < n; ++i) {
                a[i+1] = GetTaste(i);
            }
            ll sumL = 0;
            for (int i = 1; i <= n; ++i) {
                sumL += a[i];
                pref[i] = max(pref[i-1], sumL);
            }

            ll sumR = 0;
            for (int i = n; i >= 1; --i) {
                sumR += a[i];
                suf[i] = max(suf[i+1], sumR);
            }
            ll res = 0;
            for (int i = 1; i <= n; ++i) {
                res = max(res, pref[i] + suf[i+1]);
            }
            cout << res << endl;
        }
        return 0;
    }

    /* reading values in my range */
    for (int i = 1, idx = A; idx <= B; ++i, ++idx) {
        a[i] = GetTaste(idx);
    }

    ll sumL = 0;
    ll sumR = 0;
    ll bestPref = 0;
    ll bestSuf = 0;
    if (id > 0) {
        Receive(id-1);
        sumL = GetLL(id-1);
        bestPref = GetLL(id-1);
    }

    /* forwarding right */
    {
        ll sumLWithMe = sumL;
        pref[0] = bestPref;
        for (int i = 1; i <= mylen; ++i) {
            sumLWithMe += a[i];
            pref[i] = max(pref[i-1], sumLWithMe);
        }
        if (id < m-1) {
            PutLL(id+1, sumLWithMe);
            PutLL(id+1, pref[mylen]);
        }
        if (id < m-1) {
            Send(id+1);
        }
    }

    if (id < m-1) {
        Receive(id+1);
        sumR = GetLL(id+1);
        bestSuf = GetLL(id+1);
    }

    /* forwarding left */

    {
        ll sumRWithMe = sumR;
        suf[mylen+1] = bestSuf;
        for (int i = mylen; i >= 1; --i) {
            sumRWithMe += a[i];
            suf[i] = max(suf[i+1], sumRWithMe);
        }
        if (id > 0) {
            PutLL(id-1, sumRWithMe);
            PutLL(id-1, suf[1]);
        }
        if (id > 0) {
            Send(id-1);
        }
    }

    ll res = 0;
    for (int i = 1; i <= mylen; ++i) {
        res = max(res, pref[i] + suf[i+1]);
    }
    if (id != MASTER) {
        PutLL(MASTER, res);
        Send(MASTER);
    } else {
        for (int i = 0; i < m; ++i) {
            if (i == MASTER) continue;
            Receive(i);
            ll tmp = GetLL(i);
            res = max(res, tmp);
        }
        cout << res << endl;

    }
    return 0;
}