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
#include <iostream>
#include <cassert>
#include <cstdlib>
#include <cstdio>
#include <cstdint>
#include <tuple>
#include <vector>
using namespace std;
#include "kanapka.h"
#include "message.h"


template <class F>
tuple<int64_t,int64_t,int64_t,int64_t> taste( uint64_t first, uint64_t last, F f )
{
    if (first>=last) return make_tuple(0,0,0,0);

    int64_t suma=0;
    int64_t max_delta=0;
    int64_t maxx=0;
    int64_t minn=10000000000000000llu;
    bool is_max_last=true;

    for (uint64_t i =first ; i<last; i++)
    {
        suma+=f(i);//GetTaste(i);
        if (maxx-suma > max_delta) max_delta = maxx-suma;
        if (suma>maxx) {
            maxx=suma;
            is_max_last=true;
        }
        if (suma<minn){
            minn=suma;
            is_max_last=false;
        }
    }
    int64_t a=maxx,b=minn;
    if (is_max_last) swap(a,b);
    return make_tuple(max_delta, a,b-a,suma-b );
}



int main()
{


    int N = NumberOfNodes();
    int node = MyNodeId();
    {
        uint64_t ii = (node*GetN())/N;
        uint64_t jj = ((node+1)*GetN())/N;

        int64_t a,b,c,delta;
        tie(delta, a,b,c) = taste(ii,jj,  GetTaste ) ;

        PutLL(0, delta);
        PutLL(0, a);
        PutLL(0, b);
        PutLL(0, c);
        Send(0);
    }

    if (node==0)
    {
        int64_t maxdelta=0;
        vector <int64_t> tab;
        int64_t a,b,c,delta;
        for (int i=0;i<N;i++)
        {
             Receive(i);
             delta =GetLL(i);
             a     =GetLL(i);
             b     =GetLL(i);
             c     =GetLL(i);
             maxdelta = max(delta, maxdelta);
             tab.push_back(a);
             tab.push_back(b);
             tab.push_back(c);
             tie(delta, a,b,c) = taste(0,tab.size(),  [&tab](uint64_t i) { return tab[i]; } ) ;
        }
        cout<<a+b+c+max(delta,maxdelta)<<endl; //suma minus najgorsza podkanapka
    }
    return 0;
}