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
#include "kanapka.h"
#include "message.h"

#include <iostream>
#include <algorithm>
using namespace std;
long long la[5000000];
long long pa[5000000];
int main() {
   
    long long numberOfNodes = NumberOfNodes();
    long long myNodeId = MyNodeId();
    long long N = GetN();
    numberOfNodes = min(numberOfNodes,N);
    if(myNodeId>numberOfNodes-1)return 0;
    long long last = N%numberOfNodes;

    long long ile = (N - last)/numberOfNodes;

    long long a = ile*myNodeId;
    long long b = ile*(myNodeId+1)-1;
   if(numberOfNodes ==myNodeId+1 ) b+=last;

    long long prawy = 0;
    long long lewy = 0;
    long long suma = 0;
    long long best = 0;
    
    long long c = a;
    long long t = GetTaste(c);
    la[c-a] = max(0ll,t);
    c++;
    suma = t;
    while(c<=b){
        t = GetTaste(c);
        suma+=t;
        la[c-a] = max(la[c-1-a],suma);
        
        c++;
    }
    lewy = la[b-a];
    c = b;
    long long suma2 = 0;
    t = GetTaste(c);
    pa[c-a] = max(0ll,t);
    c--;
    suma2 = t;
    best = max(best,pa[0]);
    while(c>=a){
        long long t = GetTaste(c);
        suma2+=t;
        pa[c-a] = max(pa[c+1-a],suma2);
        c--;
    }
     prawy = pa[0];
    c=a;
    while(c<b){
        best = max(best,la[c-a]+pa[c+1-a]);
        c++;
    }
   /* cout<<"a( "<<myNodeId<< " ): "<<a<<"\n";
    cout<<"b( "<<myNodeId<< " ): "<<b<<"\n";
    cout<<"best( "<<myNodeId<< " ): "<<best<<"\n";*/
    if(myNodeId>0){
        PutLL(0,lewy);
        PutLL(0,prawy);
        PutLL(0,suma);
        PutLL(0,best);
        Send(0);
    }else{
        long long t_l[numberOfNodes];
        long long t_p[numberOfNodes];
        long long t_s[numberOfNodes];
        long long t_b[numberOfNodes];
        //cout<<"lewy: "<<lewy<<"\n";
        t_l[0] = lewy;
        t_p[0] = prawy;
        t_s[0] = suma;
        t_b[0] = best;
        for(int i=1;i<numberOfNodes;i++){
            Receive(i);
            t_l[i] = GetLL(i);
            t_p[i] = GetLL(i);
            t_s[i] = GetLL(i);
            t_b[i] = GetLL(i);
        }


        long long L[numberOfNodes];
        long long P[numberOfNodes];
        long long SL[numberOfNodes];
        long long SP[numberOfNodes];
        suma2=0;
        L[0] = t_l[0];
        SL[0] = t_s[0];
        suma2+=t_s[0];
        for(int i=1;i<numberOfNodes;i++){
            L[i] = max(L[i-1],suma2+t_l[i]);
            suma2+=t_s[i];
            SL[i]=suma2;
        }
        suma2=0;
        P[numberOfNodes-1] = t_p[numberOfNodes-1];
        suma2+=t_s[numberOfNodes-1];
        SP[numberOfNodes-1] = suma2;
        for(int i=numberOfNodes-2;i>=0;i--){
            P[i] = max(P[i+1],suma2+t_p[i]);
            suma2+=t_s[i];
            SP[i] = suma2;
        }
        long long w = 0;
      // for(int i=0;i<numberOfNodes;i++)cout<<"L["<<i<<"]: "<<L[i]<<"\n";
      // for(int i=0;i<numberOfNodes;i++)cout<<"P["<<i<<"]: "<<P[i]<<"\n";
        for(int i=0;i<numberOfNodes-1;i++){
            w = max(w,L[i]+P[i+1]);
        }

        if(numberOfNodes ==1)
            w=max(w,t_b[0]);
        else if(numberOfNodes ==2){
            w=max(w,SL[0]+t_b[1]);
            w=max(w,SP[1]+t_b[0]);
        }
    
        for(int i=1;i<numberOfNodes-1;i++){
            w = max(w,SL[i-1]+t_b[i]+SP[i+1]);
        }
        cout<<w<<"\n";
    }

 
  return 0;
}