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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
// DO NOT READ THIS CODE -> It's ugly
#include <bits/stdc++.h>
#include "kanapka.h"
#include "message.h"

#define REP(i,x) for(uint32 i = 0 ; i < (x) ; i++)

#define DEBUG_MODE
#ifdef DEBUG_MODE
#define print(x) cout << #x << " = " << x << endl
#define debug(x) x
#else
#define print(x)
#define debug(x)
#endif

#define hash_map      unordered_map
#define hash_multimap unordered_multimap
#define hash_set      unordered_set
#define hash_multiset unordered_multiset

using namespace std;

typedef short int int16;
typedef unsigned short int uint16;
typedef int int32;
typedef unsigned int uint32;
typedef long long int64;
typedef unsigned long long uint64;

typedef pair <int, int> PII;
typedef pair <uint32, uint32> PUU;
typedef pair <uint32, int> PUI;
typedef pair <int, uint32> PIU;
typedef pair <int64, int64> PLL;
typedef pair <int64, int> PLI;
typedef pair <int, int64> PIL;
typedef pair <int, int16> PIS;
typedef pair <int16, int> PSI;


struct Point
{
    int i; 
    int64 max;
    int64 act;
};

int64 max(int64 a, int64 b)
{
    return((a > b)? a : b);
}

class Application
{
public:
    inline void Run();
private:
    // Methods
    inline void LoadData();
    inline void Solve();
    
    // Fields
    int64 begin_;
    int64 end_;
    int64 my_id_;
    int64 instances_number_;
    int64 size_;
    
};

int main()
{
    ios_base::sync_with_stdio(false);
    Application app;
    app.Run();
}

inline void Application::LoadData()
{
    int element_number = GetN();
    my_id_ = MyNodeId();
    instances_number_ = NumberOfNodes();
    
    begin_ = (my_id_ * element_number) / instances_number_;
    end_ = ((my_id_ + 1) * element_number) / instances_number_;
    size_ = end_ - begin_;
    
    //print(begin_);
    //print(end_);
	
}

inline void Application::Solve()
{
    // small number of instances
    
    if(GetN() < 1000)
    {
        if(my_id_ != 0)
            return;
        else
        {
            int N = GetN();
            
            vector <int64> SL(N);
            vector <int64> SR(N);
            vector <int64> ML(N);
            vector <int64> MR(N);
            
            SL[0] = GetTaste(0);
            ML[0] = max(0, SL[0]);
            
            for(int i = 1; i < N; ++i)
            {
                SL[i] = SL[i - 1] + GetTaste(i);
                ML[i] = max(ML[i - 1], SL[i]);
            }
            
            SR[N - 1] = GetTaste(N - 1);
            MR[N - 1] = max(SR[N - 1], 0);
            
            for(int i = N - 2; i >= 0; --i)
            {
                SR[i] = SR[i + 1] + GetTaste(i);
                MR[i] = max(MR[i + 1], SR[i]);
            }
            
            int64 result = max(MR[0], ML[N - 1]);
            
            for(int i = 0; i < N - 1; ++i)
            {
                result = max(result, ML[i] + MR[i + 1]);
            }
            
            cout << result << "\n";
            return;
        }
               
    }
    
    int64 influence = 0;
    int64 max_left = 0, max_right = 0;
    
    for(int i = begin_; i < end_; ++i)
    {
        influence += GetTaste(i);
        if(influence > max_left)
            max_left = influence;
    }
    
    int64 temp = 0;
    for(int i = end_ - 1; i >= begin_; --i)
    {
        temp += GetTaste(i);
        if(temp > max_right)
            max_right = temp;
    }
    
    // Send (yea additional function sucks, let's do all in main ...)
    int64 MAXL = 0;
    int64 MAXR = 0;
    int64 infL = 0;
    int64 infR = 0;
            
    if(my_id_ == 0)
    {
        PutLL(1, influence);
        PutLL(1, max_left);
        Send(1);
    }
    else
    {
        Receive(my_id_ - 1);
        infL = GetLL(my_id_ - 1);
        MAXL = GetLL(my_id_ - 1);
        
        if(my_id_ != instances_number_ - 1)
        {
            PutLL(my_id_ + 1, infL + influence);
            PutLL(my_id_ + 1, max(MAXL, max_left + infL));
            Send(my_id_ + 1);
        }
    }
    
    if(my_id_ == instances_number_ - 1)
    {
        PutLL(my_id_ - 1, influence);
        PutLL(my_id_ - 1, max_right);
        Send(my_id_ - 1);
    }
    else
    {
        Receive(my_id_ + 1);
        infR = GetLL(my_id_ + 1);
        MAXR = GetLL(my_id_ + 1);
        
        if(my_id_ != 0)
        {
            PutLL(my_id_ - 1, infR + influence);
            PutLL(my_id_ - 1, max(MAXR, max_right + infR));
            Send(my_id_ - 1);
        }
    }    
    // Sended 
    

    int64 result = max(result, max(MAXR, max_right + infR) + MAXL );
    result = max(result, max(MAXL, max_left + infL) + MAXR);
    {
        
        stack <Point> points;
        stack <int64> MR;

        int64 act_sum = infR;
        int64 last = MAXR;

        
        int counter = 999999;
        int last_i = begin_;
        Point temp;
        
     
        
        for(int i = end_ - 1; i > begin_; --i)
        {
            act_sum += GetTaste(i);
            last = max(last, act_sum);

            ++counter;
            if(counter % 1000000 == 0)
            {
                temp.act = act_sum;
                temp.i = i;
                temp.max = last;
                points.push(temp);
                counter = 0;
            }
        }
        
      
        
        points.pop();
        MR.push(temp.max);
        for(int i = temp.i - 1; i > begin_; --i)
        {
            temp.act += GetTaste(i);
            temp.max = max(temp.max, temp.act);
            MR.push(temp.max);
        }    
        last_i = temp.i;
         
        last = MAXL;
        act_sum = infL;
        for(int i = begin_; i < end_ - 1; ++i)
        {
            if(MR.empty())
            {
                temp = points.top();
                points.pop();
                
                MR.push(temp.max);
                for(int j = temp.i - 1; j > last_i; --j)
                {
                    temp.act += GetTaste(j);
                    temp.max = max(temp.max, temp.act);
                    MR.push(temp.max);
                }
                
                last_i = temp.i;
            }
            
            act_sum += GetTaste(i);
            last = max(last, act_sum);
         
            result = max(result, last + MR.top());
            MR.pop();
        }
    }
    
    if(my_id_ != 0)
    {
        PutLL(0, result);
        Send(0);
    }
    else
    {
        for(int i = 1; i < instances_number_; ++i)
        {
            Receive(i);
            result = max(result, GetLL(i));
        }
        
        cout << result << "\n";
    }
    
    
}


inline void Application::Run()
{
    LoadData();
    Solve();
}