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
//
//  main.cpp
//  Kanapka
//
//  Created by Cezary Chodun on 11/16/17.
//  Copyright © 2017 CCH. All rights reserved.
//

#include <algorithm>
#include <iostream>
#include <vector>
#include <bitset>
#include <cassert>
#include "message.h"
#include "kanapka.h"

using namespace std;
#define int2 pair<int, int>
#define x first
#define y second
#define mp make_pair
#define ll long long

struct tripl{
    ll left;
    ll right;
    ll sum;
    
    tripl(){}
    tripl(ll a, ll b, ll c){
        left = a;
        right = b;
        sum = c;
    }
};

const int size = 1 * 1000 * 1000 * 100 + 11;
long long offset;

int id = -1;
int nodes = -1;
long long N;

void getNodeNum(){    id = MyNodeId(); nodes = NumberOfNodes();  N = GetN(); offset = N/nodes;  }

tripl readTripl(int i){
    tripl out;
    
    out.left = GetLL(i);
    out.right = GetLL(i);
    out.sum = GetLL(i);
    
    return out;
}

void writeTripl(int i, tripl t){
    PutLL(i, t.left);
    PutLL(i, t.right);
    PutLL(i, t.sum);
}

long long tab[size];

tripl getInfo(int start, int len){
    
    long long l = 0, r = 0;
    long long bl = 0, br = 0;
    long long sum = 0;
    
    for(int i = 0; i < len; i++){
        ll a, b;
        a = GetTaste(start+i);
        b = GetTaste(start+len-i-1);
        
        l += a;
        bl = min(bl, l);
        
        sum += a;
        
        r += b;
        br = min(br, r);
    }
    
    return tripl(bl, br, sum);
}

void empAns(tripl o){
    PutInt(0, id);
    writeTripl(0, o);
    
    Send(0);
}

void employee(){
    
    ll start = offset*id;
    
    tripl data = getInfo((int)start, (int)offset);
    
    empAns(data);
    
    //while(waitForReply());
}

vector<tripl> val;
void manager(){
    
    val.resize(nodes+1);
    val[0] = getInfo(0, (int)offset);
    tripl lst = getInfo((int)offset*nodes, N%nodes);
    
    for(int i = 1; i < nodes; i++){
        Receive(i);
        
        int inID = GetInt(i);
        if(i != inID)
            cout << "Ooops\n";
        
        val[i] = readTripl(i);
    }
    
    val.back() = lst;
    
    ll best = 0;
    ll ssum = 0;
    
    for(int i = 0; i < nodes+1; i++){
        ssum += val[i].sum;
        best = min(best, val[i].sum);
        best = min(best, val[i].left);
        best = min(best, val[i].right);
        
        ll rb = val[i].right;
        
        for(int j = i+1; j < nodes+1; j++){
            best = min(best, rb+val[j].left);
            
            rb += val[j].sum;
        }
    }
    
    cout << ssum - best << "\n";
}

int main(int argc, const char * argv[]) {
    
    fprintf(stderr, "Hello\n");
    
    getNodeNum();
    
    if(id == 0)
        manager();
    else
        employee();
    
    return 0;
}