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
#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 "message.h"
#include "maklib.h"
using namespace std;
#define pb push_back
#define mp make_pair
#define pii pair<int,int>
#define vi vector<int>
#define SZ(x) ((int)(x.size()))
#define fi first
#define se second
#define FOR(i,n) for(int (i)=0;(i)<(n);++(i))
#define FORI(i,n) for(int (i)=1;(i)<=(n);++(i))
#define IN(x,y) ((y).find((x))!=(y).end())
#define ALL(t) t.begin(),t.end()
#define FOREACH(i,t) for (typeof(t.begin()) i=t.begin(); i!=t.end(); i++)
#define REP(i,a,b) for(int (i)=(a);(i)<=(b);++i)
#define REPD(i,a,b) for(int (i)=(a); (i)>=(b);--i)
#define REMAX(a,b) (a)=max((a),(b));
#define REMIN(a,b) (a)=min((a),(b));
#define DBG cerr << "debug here" << endl;
#define DBGV(vari) cerr << #vari<< " = "<< (vari) <<endl;

typedef long long ll;
typedef double fl;

int main()
{
    int N = Size();
    int K = NumberOfNodes();
    int M = ceil(N / (double) K);

    ll maxFromLeft = 0, maxFromRight = 0, sum;
    ll acc = 0;
    for(int i = MyNodeId() *  M; i < min((MyNodeId() + 1) * M, N); ++i)
    {
        acc += ElementAt(i + 1);
        maxFromLeft = REMAX(maxFromLeft, acc);
    }
    sum = acc;
    acc = 0;
    for(int i = min((MyNodeId() + 1) * M - 1, N - 1); i >= MyNodeId() * M; --i)
    {
        acc += ElementAt(i + 1);
        maxFromRight = REMAX(maxFromRight, acc);
    }
    ll maxSoFar = 0;
    acc = 0;
    for(int i = MyNodeId() *  M; i < min((MyNodeId() + 1) * M, N); ++i)
    {
        acc = max(0LL, acc + ElementAt(i + 1));
        maxSoFar = REMAX(maxSoFar, acc);
    }
    if(MyNodeId() > 0)
    {
        PutLL(0, maxSoFar);
        PutLL(0, maxFromLeft);
        PutLL(0, maxFromRight);
        PutLL(0, sum);
        Send(0);
    }
    else
    {
        ll res = 0;
        vector<ll> mx;
        vector<ll> ml;
        vector<ll> mr;
        vector<ll> ms;

        mx.pb(maxSoFar);
        ml.pb(maxFromLeft);
        mr.pb(maxFromRight);
        ms.pb(sum);
        for(int i = 1; i < K; ++i)
        {
            Receive(i);
            mx.pb(GetLL(i));
            ml.pb(GetLL(i));
            mr.pb(GetLL(i));
            ms.pb(GetLL(i));
        }
        FOR(i, K)
        {
            REMAX(res, mx[i]);
        }
        FOR(i, K)
        {
            REP(j, i + 1, K - 1)
            {
                ll tmp = mr[i] + ml[j];
                REP(k, i + 1, j - 1) tmp += ms[k];
                REMAX(res, tmp);
            }
        }
        cout << res << endl;
    }
    return 0;
}