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

using namespace std;

long long maks[1007], mini[1007], suma[1007];

int main()
{
    long long akt=0,sp=0,ma=0,best=0;
    long long n=GetN();
    long long m=n;
    while(n%10!=0) n++;
    long long poc=(MyNodeId() * n) / NumberOfNodes();
    long long kon=((MyNodeId() + 1) * n) / NumberOfNodes();
    long long mi;
    if(poc<m){
        mi=GetTaste(poc);
        if(poc!=0) ma=mi;
    }
    else mi=0;
    //cout<<poc<<" "<<kon<<endl;
    for(long long i=poc; i<kon; i++)
    {
        if(i<m){
            akt=GetTaste(i);
        }
        else akt=0;
        sp=sp+akt;
        if(sp-ma<best)
            best=sp-ma;
        ma=max(ma,sp);
        mi=min(mi,sp);
    }
    if(MyNodeId()>0)
    {
        PutLL(0,ma);
        PutLL(0,mi);
        PutLL(0,sp);
        PutLL(0,best);
        Send(0);
    }
    else
    {
        maks[0]=ma;
        mini[0]=mi;
        suma[0]=sp;
        //cout<<best<<endl;
        //cout<<0<<" "<<maks[0]<<" "<<mini[0]<<" "<<suma[0]<<endl;
        for(int i=1; i<NumberOfNodes(); i++)
        {
            int id=Receive(i);
            maks[i]=suma[i-1]+GetLL(id);
            mini[i]=suma[i-1]+GetLL(id);
            suma[i]=suma[i-1]+GetLL(id);
            best=min(best,GetLL(id));
            sp=suma[i];
            //cout<<i<<" "<<maks[i]<<" "<<mini[i]<<" "<<suma[i]<<endl;
            //cout<<mi<<" "<<ma<<" "<<best<<" "<<sp<<endl;
            if(best>mini[i]-ma)
                best=mini[i]-ma;
            ma=max(ma,maks[i]);
        }
        cout<<sp-best;
    }

	return 0;
}