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
//Krzysztof Pieprzak
#include <iostream>
#include <fstream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <cmath>
#include <string>
#include <cstring>
#include <vector>
#include <queue>
#include <set>
#include <list>
#include <deque>
#include <stack>
#include <map>
#include <ctime>
#include <cassert>

#include "maklib.h"
#include "message.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 mySize(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 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 MAX_N = 200;

struct Trojka
{
    ll pref;
    ll suf;
    ll sum;
} t[MAX_N];

int N;
int ID;

vll pref;
vll mx;

ll getSum(int a, int b)
{
    ll res = t[a].suf + t[b].pref;
    FOR (i, a+1, b) res += t[i].sum;

    return res;
}

void rob()
{
    int n = Size();
    int nn = n/N;

    int bg = nn*ID+1;
    int ed = (ID == N-1 ? n+1 : bg+nn);

    pref.PUB(ElementAt(bg));
    FOR (i, bg+1, ed) pref.PUB(pref.back()+ElementAt(i));

    mx.resize(mySize(pref));
    mx[ed-bg-1] = pref[ed-bg-1];
    FORREV (i, bg, ed-2) mx[i-bg] = max(mx[i-bg+1],pref[i-bg]);

    ll resMx = 0;
    FOR (i, bg, ed) resMx = max(resMx, mx[i-bg]-pref[i-bg]);

    ll resMxPref = 0;
    FOR (i, bg, ed) resMxPref = max(resMxPref, pref[i-bg]);

    ll resMxSuf = 0;
    ll cur = 0;
    FORREV (i, bg, ed-1)
    {
        cur += ElementAt(i);
        resMxSuf = max(resMxSuf, cur);
    }

    if (MyNodeId() > 0)
    {
        PutLL(0, resMx);
        PutLL(0, resMxPref);
        PutLL(0, resMxSuf);
        PutLL(0, cur);
        Send(0);
    }
    else
    {
        t[0].pref = resMxPref;
        t[0].suf = resMxSuf;
        t[0].sum = cur;

        //cout << "0: " << "(" << t[0].pref << "," << t[0].suf << "," << t[0].sum << ")\n";

        FOR (instancja, 1, N)
        {
            Receive(instancja);
            resMx = max(resMx, GetLL(instancja));
            t[instancja].pref = GetLL(instancja);
            t[instancja].suf = GetLL(instancja);
            t[instancja].sum = GetLL(instancja);

            //cout << instancja << ": " << "(" << t[instancja].pref << "," << t[instancja].suf << "," << t[instancja].sum << ")\n";
        }

        FOR (i, 0, N-1)
            FOR (j, i+1, N)
                resMx = max(resMx, getSum(i,j));

        cout << resMx << endl;
    }
}

int main()
{
    N = NumberOfNodes();
    ID = MyNodeId();

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

    while (test--) rob();

    return 0;
}