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
#include <iostream>
#include "message.h"
#include "kanapka.h"
using namespace std;

#define REP(a,n) for(int a=0;a<n;++a)
#define LL long long

int main(){
    int n=GetN();
    int m=NumberOfNodes();
    int id=MyNodeId();

    //if(MyNodeId()==0){
    //    for(int a=0;a<n;++a){
    //        cout<<GetTaste(a)<<" ";
    //    }
    //    cout<<endl;
    //}

    int d=(n+m-1)/m;

    LL sum=0;
    int l=d*id;
    int r=min(d*(id+1),n) -1;

    //cout<<id<<": "<<l<<" "<<r<<endl;

    LL lmaxLoc=0;
    LL rmaxLoc=0;
    for(int a=l;a<=r;++a){
        sum+=GetTaste(a);
        lmaxLoc=max(lmaxLoc,sum);
    }
    //cout<<id<<"sum: "<<sum<<endl;
    {
        LL sumr=0;
        for(int a=r;a>=l;--a){
            sumr+=GetTaste(a);
            rmaxLoc=max(rmaxLoc,sumr);
            //cout<<sumr<<endl;
        }
    }

    LL lsum=0;
    if(id>0){
        Receive(id-1);
        lsum=GetLL(id-1);
    }
    if(id==0 || id<m-1){
        PutLL(id+1,lsum+sum);
        Send(id+1);
    }

    LL rsum=0;
    if(id<m-1){
        Receive(id+1);
        rsum=GetLL(id+1);
    }
    if(id==m-1 || id>0){
        PutLL(id-1,rsum+sum);
        Send(id-1);
    }

    //cout<<id<<"(lsum,rsum): ("<<lsum<<","<<rsum<<")\n";
    //cout<<id<<"(lmax,rmax)Loc: ("<<lmaxLoc<<","<<rmaxLoc<<")\n";

    //update lmax and rmax
    LL lmax=0;
    if(id>0){
        Receive(id-1);
        lmax=GetLL(id-1);
    }
    if(id==0 || id<m-1){
        PutLL(id+1,max(lmax,lsum+lmaxLoc));
        Send(id+1);
    }

    LL rmax=0;
    if(id<m-1){
        Receive(id+1);
        rmax=GetLL(id+1);
    }
    if(id==m-1 || id>0){
        PutLL(id-1,max(rmax,rsum+rmaxLoc));
        Send(id-1);
    }

    //cout<<id<<"(lmax,rmax): ("<<lmax<<","<<rmax<<")\n";

    // answer (4 cases)
    LL ans=lmax+rmax;
    ans=max(ans,lmax+rmaxLoc+rsum);
    ans=max(ans,rmax+lmaxLoc+lsum);
    LL ret=0;
    {
        LL min1=0;
        LL sum1=0;
        for(int a=l;a<=r;++a){
            sum1+=-GetTaste(a);
            min1=min(sum1,min1);
            ret=max(ret,sum1-min1);
        }
        //cout<<id<<": "<<ret<<endl;
    }
    //cout<<id<<"sum,ret,rsum,lsum: "<<sum<<" "<<ret<<" "<<rsum<<" "<<lsum<<endl;
    ans=max(ans,sum+ret+rsum+lsum);

    if(MyNodeId()==0){
        for(int a=1;a<m;++a){
            Receive(a);
            ans=max(ans,GetLL(a));
        }
        cout<<ans;
    }else{
        PutLL(0,ans);
        Send(0);
    }
    return 0;
}