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

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

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


#ifdef HOME
////////////////////////////////////////////////////////
int NumberOfNodes() { return 1; }
int MyNodeId() { return 0; }
void PutChar(int target, char value) {}
void PutInt(int target, int value) {}
void PutLL(int target, long long value) {}
void Send(int target) {}
int Receive(int source) { return 0; }
char GetChar(int source) { return 0; }
int GetInt(int source) { return 0; }
long long GetLL(int source) { return 0; }
////////////////////////////////////////////////////////
long long GetN() { return 3; }
long long GetTaste(long long i) {
    long long data[] = {1, -2, 4};
    return data[i++];
}
////////////////////////////////////////////////////////
#else
#include "kanapka.h"
#include "message.h"
#endif


int non; // number of nodes
int mni; // my node id
long long n;

void init() {
    non = NumberOfNodes();
    mni = MyNodeId();
    n = GetN();
}

long long rangeStart(int id) {
    long long res = n / non * id;
    int rest = n % non;
    if (non - id < rest)
        res += rest + id - non;
    return res;
}

#define sss(x,y) (s[(y)] - ((x) == 0 ? 0 : s[(x)-1]))

int main() {
    init();

    long long aa, bb, ss, rr;
    aa = ss = rr = 0;
    bb = -5000000000000000000LL;

    long long x = rangeStart(mni);
    long long y = rangeStart(mni + 1);

    for (long long i = x; i < y; ++i) {
        long long cur = GetTaste(i);
        ss += cur;
        aa = std::min(aa, ss);
        bb = std::max(bb, ss);
    }
    bb = ss - bb;
    aa = std::min(aa, 0LL);
    bb = std::min(bb, 0LL);

    // kadane algorithm
    long long cur = 0;
    for (long long i = x; i < y; ++i) {
        cur = std::max(0LL, cur + GetTaste(i));
        rr = std::max(rr, cur);
    }

    if (mni > 0) {
        PutLL(0, aa);
        PutLL(0, bb);
        PutLL(0, ss);
        PutLL(0, rr);
        Send(0);
        return 0;
    }

    vector<long long> a(non), b(non), s(non), r(non);
    a[0] = aa;
    b[0] = bb;
    s[0] = ss;
    r[0] = rr;

    long long sum = ss;
    long long res = rr;

    REP(i, non - 1) {
        int id = Receive(-1);
        a[id] = GetLL(id);
        b[id] = GetLL(id);
        s[id] = GetLL(id);
        r[id] = GetLL(id);
        sum += s[id];
        res = std::min(res, r[i]);
    }

    FOR(i, 1, non-1) s[i] += s[i-1];

    REP(i, non) {
        res = std::min(res, b[i] + sss(i+1, non-1));
    }

    REP(i, non) {
        res = std::min(res, a[i] + sss(0, i-1));
    }

    REP(i, non) FOR(j, i+1, non-1) {
        res = std::min(res, b[i] + a[j] + sss(i+1, j-1));
    }

    printf("%lld\n", sss(0, non-1) - res);

    return 0;
}