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
#include <cstdio>
#include <algorithm>
#include "message.h"
#include "maklib.h"
using namespace std;

#define LL long long

int main()
{
    int l=(MyNodeId()*Size())/NumberOfNodes();
    int r=((MyNodeId()+1)*Size())/NumberOfNodes();
    LL sum=0,sum1=0;
    for(int a=l;a<r;++a)sum+=ElementAt(a+1);

    if(MyNodeId()!=0)
    {
        Receive(MyNodeId()-1);
        sum1=GetLL(MyNodeId()-1);
    }
    if(MyNodeId()!=NumberOfNodes()-1)
    {
        PutLL(MyNodeId()+1,sum+sum1);
        Send(MyNodeId()+1);
    }

    LL down=0,msa=0,mini=sum1,maxi=sum1;
    LL add=-sum1;
    for(int a=l;a<r;++a)
    {
        sum1+=ElementAt(a+1);
        maxi=max(maxi,sum1);
        mini=min(mini,sum1);

        LL elem=sum1+add;
        msa=max(msa,elem);
        if(elem<down)add+=down-elem;
    }

    if(MyNodeId()!=0)
    {
        Receive(MyNodeId()-1);
        LL mini1=GetLL(MyNodeId()-1);
        LL msa1=GetLL(MyNodeId()-1);
        msa=max(msa,max(msa1,maxi-mini1));
        mini=min(mini,mini1);
    }
    if(MyNodeId()!=NumberOfNodes()-1)
    {
        PutLL(MyNodeId()+1,mini);
        PutLL(MyNodeId()+1,msa);
        Send(MyNodeId()+1);
    }else
    {
        printf("%lld",msa);
    }
    return 0;
}