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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
#include <bits/stdc++.h>

using namespace std;

#define scanf(...) scanf(__VA_ARGS__)?0:1

#include "kanapka.h"
#include "message.h"

#define PB push_back
#define MP make_pair
#define fst first
#define snd second
#define LL long long
#define int LL
#define FOR(i,a,b) for (int i = (a); i <= (b); i++)
#define ALL(x) (x).begin(), (x).end()
#define SZ(x) ((int)(x).size())
#define VI vector<int>
#define PII pair<int,int>
#define LD long double

template<class C> void minu(C& a4, C b4) { a4 = min(a4, b4); }
template<class C> void maxu(C& a4, C b4) { a4 = max(a4, b4); }

template<class TH> void _dbg(const char *sdbg, TH h){cerr<<sdbg<<"="<<h<<"\n";}
template<class TH, class... TA> void _dbg(const char *sdbg, TH h, TA... a) {
  while(*sdbg!=',')cerr<<*sdbg++;cerr<<"="<<h<<","; _dbg(sdbg+1, a...);
}

template<class T> ostream &operator<<(ostream &os, vector<T> V){
  os<<"[";for(auto vv:V)os<<vv<<",";return os<<"]";
} 

template<class L, class R> ostream &operator<<(ostream &os, pair<L,R> P) {
  return os << "(" << P.fst << "," << P.snd << ")";
}


#ifdef LOCAL
#define debug(...) _dbg(#__VA_ARGS__, __VA_ARGS__)
#else
#define debug(...) (__VA_ARGS__)
#define cerr if(0)cout
#endif

using namespace std;

int mxpref[13000000];

int32_t main()
{
    int n = GetN();
    int nodes = NumberOfNodes();
    int id = MyNodeId();
    int sum = 0;
    int wyn = 0;
    int worspref = 0;
    int bestpref = 0;
    int beg = n * id / nodes, end = n * (id + 1) / nodes;
    for(int i = beg; i < end; i++)
    {
        int tmp = GetTaste(i);
        sum += tmp;
        minu(worspref, sum);
        maxu(bestpref, sum);
        if((i - beg)%8 == 0) mxpref[(i - beg)/8]=(bestpref);
    }
    minu(wyn, sum);
    int tyl_sum = 0;
    int worssuff = 0;
    int bestsuff = 0;
    vector <int> buf;
    for(int i = end - 1; i >= beg; i--)
    {
        //if(id==8)debug(i);
        int tmp = GetTaste(i);
        tyl_sum += tmp;
        minu(worssuff, tyl_sum);
        maxu(bestsuff, tyl_sum);
        if(i - beg> 1)
        {
            //if(id==8)debug("KURWA", i);
            int nr = i - 1;
            if(buf.empty())
            {
                int star = (nr & (~7));
                VI vals;
                int sumpoc = tmp;
                for(int j = star + 1; j <= nr; j++) 
                {
                    vals.PB(GetTaste(j));
                    sumpoc += vals.back();
                }
                sumpoc = sum - sumpoc;
                buf.PB(mxpref[nr>>3]);
                int bestpre = mxpref[nr>>3];
                for(int j = star + 1; j <= nr; j++) 
                {
                    sumpoc += vals.back();
                    maxu(bestpre, sumpoc);
                    buf.PB(bestpre);
                }
            }
            minu(wyn, sum - bestsuff - buf[nr - (nr & (~7))]);
            
            
            if((nr & 7) == 0) buf.clear();
        }
    }
    
        //if(id==8)debug(wyn);
    
    int pop = 0;
    if(id > 0)
    {
        Receive(id - 1);
        pop = GetLL(id - 1);
    }
    minu(wyn, pop + worspref);
    minu(worssuff, pop + sum);
    if(id < nodes - 1)
    {
        PutLL(id + 1, worssuff);
        Send(id + 1);
    }
    
    /*
    int nas = 0;
    if(id < nodes - 1)
    {
        Receive(id + 1);
        nas = GetLL(id + 1);
    }
    maxu(wyn, nas + bestpref);
    if(id > 0)
    {
        PutLL(id - 1, nas + sum);
        Send(id - 1);
    }//*/
    
    if(id > 0)
    {
        PutLL(0, sum);
        PutLL(0, wyn);
        Send(0);
    }
    else
    {
        for(int i = 1; i < nodes; i++)
        {
            Receive(i);
            sum += GetLL(i);
            minu(wyn, GetLL(i));
        }
        printf("%lld\n", sum - wyn);
    }
}