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
/* 
 * File:   mak.cpp
 * Author: baca
 *
 * Created on 10 maj 2014, 21:19
 */

#include <cstdlib>
#include <iostream>
#include <algorithm>

#include "maklib.h"
#include "message.h"

using namespace std;

/*
 * 
 */
int main(int argc, char** argv) {
    long long odPoczatkuCurrent = 0;
    long long odPoczatkuMax = 0;
    
    long long odKoncaCurrent = 0;
    long long odKoncaMax = 0;
    
    long long wSrodkuCurrent = 0;
    long long wSrodkuMax = 0;
    
    long long size = Size();
    long long nodes = min((long long) NumberOfNodes(), size);
    
    if (MyNodeId() >= size) {
        return 0;
    }
    
    long long poczatek = (MyNodeId() * size) / nodes + 1;
    long long koniec = ((MyNodeId() + 1) * size) / nodes;
    
    for (long long i = poczatek; i <= koniec; i++) {
        long long element = ElementAt(i);
        
        odPoczatkuCurrent += element;
        
        if (odPoczatkuMax < odPoczatkuCurrent) {
            odPoczatkuMax = odPoczatkuCurrent;
        }
        
        wSrodkuCurrent += element;
        
        if (wSrodkuCurrent < 0) {
            wSrodkuCurrent = 0;
        } else if (wSrodkuMax < wSrodkuCurrent) {
            wSrodkuMax = wSrodkuCurrent;
        }
    }
    
    for (long long i = koniec; i >= poczatek; i--) {
        odKoncaCurrent += (long long) ElementAt(i);
        
        if (odKoncaMax < odKoncaCurrent) {
            odKoncaMax = odKoncaCurrent;
        }
    }
    
//    cout << poczatek << " - " << koniec << ": " << odPoczatkuMax << " " << wSrodkuMax << " " << odKoncaMax << " " << odPoczatkuCurrent << endl;
    
    long long prevNaKoncu = 0;
    long long prevMax = 0;
    
    if (MyNodeId() > 0) {
        int source = Receive(MyNodeId() - 1);
        prevNaKoncu = GetLL(source);
        prevMax = GetLL(source);
    }
    
    long long naKoncu = max(odKoncaMax, prevNaKoncu + odPoczatkuCurrent);
    long long currentMax = max(wSrodkuMax, max(prevNaKoncu + odPoczatkuMax, prevMax));
    
    if (MyNodeId() == nodes - 1) {
        cout << currentMax << endl;
    } else {
        PutLL(MyNodeId() + 1, naKoncu);
        PutLL(MyNodeId() + 1, currentMax);
        Send(MyNodeId() + 1);
    }
    
    return 0;
}