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
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <ctime>
#include <vector>
#include <string>
#include <map>
#include <set>
#include <queue>
#include <algorithm>
using namespace std;

typedef long long ll;
typedef pair<int,int> pii;
typedef vector<int> vi;
typedef vector<vi> vvi;
#define mp make_pair

#define rep(i,n) for (int i=0,___=(n); i<___; ++i)
#define repn(n) rep(sth,n)
#define FOR(i,a,b) for (int i=(a),___=(b); i<=___; ++i)
#define FORD(i,a,b) for (int i=(a),___=(b); i>=___; --i)

int read() { int n; scanf("%d", &n); return n; }

#include "maklib.h"
#include "message.h"

//int ElementAt(int) { return 0; }
//int Size() { return 54; }
//int NumberOfNodes() { return 10; }
//int MyNodeId() { return 9; }
//void PutChar(int, char) {}
//void PutInt(int, int) {}
//void PutLL(int, ll) {}
//void Send(int) {}
//int Receive(int) { return 0; }
//char GetChar(int) { return 0; }
//int GetInt(int) { return 0; }
//ll GetLL(int) { return 0; }




bool signCmp(ll a, ll b) {
    if (a >= 0 && b >= 0) return true;
    if (a <= 0 && b <= 0) return true;
    return false;
}

void f(int a, int b, ll &s, ll &m, ll &l, ll &r) {
    s = 0;
    l = 0;
    r = 0;
    m = 0;
    if (a == b) return;
    for (int i=a; i<b; ++i) {
        s += ElementAt(i);
        l = std::max(l, s);
    }
    ll sr = 0;
    for (int i=b-1; i>=a; --i) {
        sr += ElementAt(i);
        r = std::max(r, sr);
    }
    vector<ll> v;
    v.push_back(ElementAt(a));
    m = std::max(m, v.back());
    for (int i=a+1; i<b; ++i) {
        if (signCmp(v.back(), ElementAt(i)))
            v.back() += ElementAt(i);
        else
            v.push_back(ElementAt(i));
        m = std::max(m, v.back());
        if (v.size() >= 3 && v[v.size() - 3] >= 0 && v[v.size() - 3] >= -v[v.size() - 2]) {
            ll x = v.back();
            v.pop_back();
            x += v.back();
            v.pop_back();
            v.back() += x;
        }
        m = std::max(m, v.back());
    }

    //fprintf(stdout, "%d: %d %d %lld %lld %lld %lld\n", MyNodeId(), a, b, s, m, l, r);
}

int main() {
    //if (Size() < NumberOfNodes()) {
        //if (MyNodeId() > 0)
            //return 0;
        //int c = 1;
        //int d = Size() + 1;
        //ll ss, mm, lll, rr;
        //f(c, d, ss, mm, lll, rr);
        //printf("%lld\n", mm);
        //return 0;
    //}

    int a = Size() / NumberOfNodes();
    int b = Size() % NumberOfNodes();
    int c, d;
    if (MyNodeId() < b) {
        c = MyNodeId() * (a+1);
        d = (MyNodeId() + 1) * (a+1);
    } else {
        c = b + MyNodeId() * a;
        d = b + (MyNodeId() + 1) * a;
    }
    c++;
    d++;

    ll ss, mm, lll, rr;
    f(c, d, ss, mm, lll ,rr);

    if (MyNodeId() > 0) {
        PutLL(0, ss);
        PutLL(0, mm);
        PutLL(0, lll);
        PutLL(0, rr);
        Send(0);
        return 0;
    }

    vector<ll> s(1, ss), m(1, mm), l(1, lll), r(1, rr);

    for (int i=1; i<NumberOfNodes(); ++i) {
        Receive(i);
        s.push_back(GetLL(i));
        m.push_back(GetLL(i));
        l.push_back(GetLL(i));
        r.push_back(GetLL(i));
    }

    ll res = 0;
    int n = NumberOfNodes();
    vector<ll> sum(n + 1);
    sum[0] = 0;
    for (int i=0; i<n; ++i) {
        if (res < m[i]) {
            res = m[i];
            //printf("M: %lld, m[%d]\n", res, i);
        }
        sum[i+1] = sum[i] + s[i];
    }
    for (int a=0; a<n; ++a) for (int b=a+1; b<n; ++b) {
        ll cur = r[a] + l[b] + sum[b] - sum[a+1];
        if (res < cur) {
            res = cur;
            //printf("N: %lld %d %d\n", res, a, b);
        }
    }

    printf("%lld\n", res);
    return 0;
}