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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
//Krzysztof Pieprzak
#include <bits/stdc++.h>
#include "message.h"
#include "kanapka.h"

using namespace std;

typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
typedef unsigned int uint;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef pair<int,int> pii;
typedef pair<long long, long long> pll;
typedef vector<pii> vpii;
typedef vector<pll> vpll;

#define Size(x) (int)x.size()
#define VAR(v,n) typeof(n)v = (n)
#define FOR(i,a,b) for(VAR(i,a); i < (b); ++i)
#define FORE(i,a,b) for(VAR(i,a); i <= (b); ++i)
#define FORREV(i,a,b) for(VAR(i,b); i >= (a); --i)
#define FORSTEP(i,a,b,step) for(VAR(i,a); i < (b); i += (step))
#define FOREACH(i,c) for(VAR(i,(c).begin()); i != (c).end(); ++i)
#define FOREACHS(i,c,n) for(VAR(i,&(c)[0]); i-(c)<(n); ++i)
#define ALL(x) x.begin(),x.end()
#define CLEAR(t) memset(t, 0, sizeof(t))
#define F first
#define S second
#define MP make_pair
#define PUB push_back
#define POB pop_back
#define pieprzu ios_base::sync_with_stdio(0);

const int    INF = 1000000001;
const double EPS = 10e-9;

const int MIN_INSTANCES = 10;
const int MAX_INSTANCES = 100;

int INSTANCE_ID;
int INSTANCES;

ll mxPref;
ll mxSuf;
ll mxPrefSuf;
ll sum;

struct Pieprzu {
    Pieprzu() {}
    Pieprzu(ll pref, ll suf, ll prefSuf, ll sum) : pref(pref), suf(suf), prefSuf(prefSuf), sum(sum) {}

    ll pref;
    ll suf;
    ll prefSuf;
    ll sum;
};

vector<Pieprzu> t;
vector<ll> sums;
vector<ll> mxPrefs;

inline void update(ll id, ll &val, ll &mx, ll &mxId) {
    val += GetTaste(id);
    if (val > mx) {
        mx = val;
        mxId = id;
    }
}

ll getMxPref(ll bg, ll ed, ll &mxPrefId) {
    ll sum = 0, mxPref = 0;
    FOR (i, bg, ed) update(i, sum, mxPref, mxPrefId);
    return mxPref;
}

ll getMxSuf(ll bg, ll ed, ll &mxSufId) {
    ll sum = 0, mxSuf = 0;
    FORREV (i, bg, ed-1) update(i, sum, mxSuf, mxSufId);
    return mxSuf;
}

ll getSum(ll bg, ll ed) {
    ll sum = 0;
    FOR (i, bg, ed) sum += GetTaste(i);
    return sum;
}

ll getMnSeq(ll bg, ll ed) {
    ll mnSeq = 0;
    ll seq = 0;
    FOR (i, bg, ed) {
        seq = GetTaste(i) + (seq < 0 ? seq : 0);
        mnSeq = min(mnSeq, seq);
    }

    return mnSeq;
}

void robInstance() {
    ll n = GetN();
    ll bg = (n / INSTANCES) * INSTANCE_ID;
    ll ed = bg + n / INSTANCES + (INSTANCE_ID == INSTANCES - 1 ? n%INSTANCES : 0);
    n = ed - bg;

    sum = getSum(bg, ed);

    ll mxPrefId = -1;
    mxPref = getMxPref(bg, ed, mxPrefId);

    ll mxSufId = n;
    mxSuf = getMxSuf(bg, ed, mxSufId);

    if (mxPrefId < mxSufId) {
        mxPrefSuf = mxPref + mxSuf;
        return;
    }

    ll tmp;

    mxPrefSuf = 0;
    //only pref
    mxPrefSuf = max(mxPrefSuf, mxPref + getMxSuf(mxPrefId + 1, ed, tmp));
    //only suf
    mxPrefSuf = max(mxPrefSuf, getMxPref(bg, mxSufId, tmp) + mxSuf);
    //both
    mxPrefSuf = max(mxPrefSuf, sum - getMnSeq(mxSufId, mxPrefId + 1));
}

void send() {
    PutLL(0, mxPref);
    PutLL(0, mxSuf);
    PutLL(0, mxPrefSuf);
    PutLL(0, sum);
    Send(0);
}

void collect() {
    t.PUB(Pieprzu(mxPref, mxSuf, mxPrefSuf, sum));
    FOR (i, 1, INSTANCES) {
        Receive(i);
        t.PUB(Pieprzu());
        t.back().pref = GetLL(i);
        t.back().suf = GetLL(i);
        t.back().prefSuf = GetLL(i);
        t.back().sum = GetLL(i);
    }
}

void combine() {
    sum = mxPref = 0;
    FOR (i, 0, INSTANCES) {
        mxPrefs.PUB(mxPref = max(mxPref, sum + t[i].pref));
        sums.PUB(sum = sum + t[i].sum);
    }

    ll mxSum = 0;
    sum = mxSuf = 0;
    FORREV (i, 0, INSTANCES-1) {
        //none
        mxSum = max(mxSum, (i > 0 ? mxPrefs[i-1] : 0) + mxSuf);
        //only pref
        mxSum = max(mxSum, (i > 0 ? sums[i-1] : 0)+t[i].pref + mxSuf);
        //only suf
        mxSum = max(mxSum, (i > 0 ? mxPrefs[i-1] : 0) + t[i].suf+sum);
        //both
        mxSum = max(mxSum, (i > 0 ? sums[i-1] : 0) + t[i].prefSuf + sum);

        mxSuf = max(mxSuf, sum+t[i].suf);
        sum += t[i].sum;
    }

    printf("%lld\n", mxSum);
}

void rob(int test) {
    robInstance();

    if (INSTANCE_ID) {
        send();
    }
    else {
        collect();
        combine();
    }
}

int main() {
    int test = 1;
    //scanf("%d", &test);

    INSTANCE_ID = MyNodeId();
    INSTANCES = NumberOfNodes();

    FORE (i, 1, test) rob(i);

    return 0;
}