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
#include <bits/stdc++.h>
#include "message.h"
#include "kanapka.h"
using namespace std;
typedef long long ll;

ll id, NN;
ll a,b,n;
ll maxNodes = 10000;


void init(){
    id = MyNodeId();
    NN = NumberOfNodes();
    n = GetN();
    if(id >= n || id > maxNodes)
        exit(0);
    NN = min(NN,n);
    NN = min(NN,maxNodes);
}

void setInterval(){
    a = (id*n/NN);
    b = ((id+1)*n/NN);
    b = min(b,n);
    b = max(b,0LL);
}

int main(){
    init();
    setInterval();

    vector<ll> prefSum(b-a,GetTaste(a));

    for(ll i=a+1,j=1;i<b;i++,j++)
        prefSum[j] = prefSum[j-1] + GetTaste(i);

    ll premia = 0;

    //send sum

    if(id == 0){
        if(NN > 1){
            PutLL(id+1,prefSum.back());
            Send(id+1);
        }
    }
    else{
        Receive(-1);
        premia = GetLL(id-1);
        if(id != NN-1){
            PutLL(id+1,premia+prefSum.back());
            Send(id+1);
        }
    }

    
    ll maxi = 0, maxiPrev = 0;
    for(int i=0;i<prefSum.size();i++){
        prefSum[i] += premia;
        maxi = max(maxi,prefSum[i]);
    }
    
    //Send Maxi

    if(id == 0 ){
        if(NN > 1){
            PutLL(id+1,maxi);
            Send(id+1);
        }
    }

    else{
        Receive(-1);
        maxiPrev = GetLL(id-1);

        if(id != NN-1){
            PutLL(id+1,max(maxi,maxiPrev));
            Send(id+1);
        }
    }
    
    ll res = 0;
    for(int i=0;i<prefSum.size();i++){
        res = min(res,prefSum[i] - maxiPrev);
        maxiPrev = max(maxiPrev,prefSum[i]);
    }

    ///Send result
    if(id == NN-1){
        PutLL(0,prefSum.back());
        Send(0);
    }
    if(id != 0){
        PutLL(0,res);
        Send(0);
    }
    else{
        Receive(NN-1);
        ll sumAll = GetLL(NN-1);
        int ct = NN-1;
        while(ct > 0){
            Receive(ct);
            res = min(res,GetLL(ct));
            ct--;
        }
        cout<<sumAll-res<<endl;
    }
}