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

#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;

void print_vector(vector<int64_t> sums){
    for (int64_t c : sums)
        std::cout << c << ' ';
    cout<<endl;
}

int64_t find_max(vector<int64_t> sums){
    int64_t currentMax =  0;
    for (int64_t c : sums)
        if(c>currentMax){
            currentMax = c;
        }
    return currentMax;
}


vector<int64_t> combine_sums(vector<int64_t> sums){
    
    if(sums.size()>2){
    bool should_continue = true;
    
    while (should_continue){
        should_continue = false;
        vector<int64_t> tmp;
        for (int i = 0 ; i < sums.size() ; i++){
            int64_t elem = sums[i];
            if(elem>=0){
                if(i< sums.size()-2){
                    int64_t three = sums[i]+sums[i+1]+sums[i+2];
                    if(three>=sums[i]&&three>=sums[i+2]){
                        tmp.push_back(three);
                        i+=2;
                        should_continue = true;
                    } else {
                        tmp.push_back(elem);
                    }
                } else {
                    tmp.push_back(elem);
                }
            } else {
                tmp.push_back(elem);
            }
            
        }
        sums = tmp;
        
    }
    }
    return sums;
}

int main(){
    int NODE_ID = MyNodeId();
    int NODE_NO = NumberOfNodes();

    int ARRAY_SIZE = Size();
    
    bool MANY_NODES = ARRAY_SIZE/NODE_NO >= 10;
    
    int START;
    int END;
    
    if(MANY_NODES){
        START = (NODE_ID*ARRAY_SIZE)/NODE_NO;
        END = ((NODE_ID+1)*ARRAY_SIZE)/NODE_NO;
    } else {
        if(NODE_ID==0){
            START = 0;
            END = ARRAY_SIZE;
        } else {
            START = 0;
            END = 0;
        }
    }
    
    vector<int64_t> sums;
    
    if(END>0){
        int64_t tmp = ElementAt(START+1);
    
        for(int i = START+2 ; i<=END;i++){
            int elem = ElementAt(i);
            if(tmp>=0){
                if(tmp+elem>=tmp){
                    tmp += elem;
                } else {
                    sums.push_back(tmp);
                    tmp = elem;
                }
            } else {
                if(tmp+elem<=tmp){
                    tmp += elem;
                } else {
                    sums.push_back(tmp);
                    tmp = elem;
                }
            
            }
        }
    
    
        sums.push_back(tmp);
    }
    
    
    sums = combine_sums(sums);
    
    if(MANY_NODES){
        if(MANY_NODES && NODE_ID>0){
            PutInt(0, sums.size());
            for(int64_t c:sums){
                PutLL(0, c);
            }
            Send(0);
        } else {
            
            for(int i = 1; i<NODE_NO; i++){
                Receive(i);
                //cout<<"instancja: "<<i<<endl;
                int how_many = GetInt(i);
                //cout<<"how_many: "<<how_many<<endl;
                for(int j = 0; j< how_many ; j++){
                    int64_t candidate = GetLL(i);
                    int64_t lastElem = sums[sums.size()-1];
                    if(candidate>=0 && lastElem >=0){
                        sums[sums.size()-1] =candidate +lastElem;
                    } else if(candidate<0 && lastElem<0){
                        sums[sums.size()-1] =candidate +lastElem;
                    } else {
                        sums.push_back(candidate);
                    }
                }
            }
            
        }
    }
    
    
    if(MANY_NODES){
        combine_sums(sums);
    }
    
    
    
    
    if(NODE_ID == 0){
        cout<<find_max(sums)<<endl;
    }

	return 0;
}