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

#include <algorithm>
#include <iostream>

using namespace std;

int n, ile, num, S;

struct elem
{
    long long suma, maks, l, p;
};
elem e1, e2, e0;

static inline int proc(int elem)
{
    return elem % ile;
}

static inline void wyslij(elem e, int nr)
{
    int pr = proc(nr);
//    cout << "proces " << num << " wysyla element numer " << nr << " do procesu " << pr << endl;
    PutLL(pr, e.suma);
    PutLL(pr, e.maks);
    PutLL(pr, e.l);
    PutLL(pr, e.p);
    Send(pr);
}

static inline elem pobierz(int nr)
{
    int pr = proc(nr);
//    cout << "proces " << num << " pobiera element numer " << nr << " od procesu " << pr << endl;
    elem e;
    Receive(pr);
    e.suma = GetLL(pr);
    e.maks = GetLL(pr);
    e.l = GetLL(pr);
    e.p = GetLL(pr);
    return e;
}

int main() {

    n = Size();
    ile = NumberOfNodes();
    num = MyNodeId();

    S = 1;
    while(S < n)
        S *= 2;

    int i = (S*2-1)/ile*ile + num;
    if(i >= 2*S)
        i -= ile;

    for(; i > 0; i -= ile)
    {
//        cout << "i: " << i << endl;
        if(i >= S)
        {
//            cout << "proces " << num << " wczytuje element numer " << i-S+1 << endl;

            if(i < S + n)
                e0.suma = ElementAt(i-S+1);
            else
                e0.suma = 0;

            e0.l = e0.p = e0.maks = max(0LL, e0.suma);

            wyslij(e0, i/2);
        }
        else
        {
            e2 = pobierz(i*2+1);
            e1 = pobierz(i*2);

            e0.maks = max(e1.p + e2.l, max(e1.maks, e2.maks));
            e0.suma = e1.suma + e2.suma;
            e0.l = max(e1.l, e1.suma + e2.l);
            e0.p = max(e2.p, e2.suma + e1.p);

//            cout << "tu" << endl;

            if(i > 1)
                wyslij(e0, i/2);
            else
                printf("%lld\n", e0.maks);
        }

//        cout << "   res[" << i << "] - suma: " << e0.suma << ", maks: " << e0.maks << ", lewo: " << e0.l << ", prawo: " << e0.p << endl;
    }

//    cout << "koniec" << endl;

    return 0;
}