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
#include "kanapka.h"
#include "message.h"
#include <bits/stdc++.h>
#define INF (1ll << 60ll)
// MAXL
using namespace std;
typedef long long ll;

struct part_solution {
    ll sum;
    ll mid;
    ll pref;
    ll post;
};

struct task {
    ll start;
    ll end;
};

void PutPart(int target, part_solution s) {
    PutLL(target, s.sum);
    PutLL(target, s.mid);
    PutLL(target, s.pref);
    PutLL(target, s.post);
}

part_solution GetPart(int source) {
    part_solution s = {
        GetLL(source),
        GetLL(source),
        GetLL(source),
        GetLL(source)
    };
    return s;
}

void PutTask(int target, task t) {
    PutLL(target, t.start);
    PutLL(target, t.end);
}
task GetTask(int source) {
    task t = {
        GetLL(source),
        GetLL(source)
    };
    return t;
}

void prints(part_solution s) {
    printf("solution (sum=%lld, mid=%lld, pref=%lld, post=%lld)\n", s.sum, s.mid ,s.pref, s.post);
}

part_solution solve(vector<ll> tab) {
    ll n = tab.size();
    vector<ll> pref(n), post(n), maxpref(n), maxpost(n);
    if (n == 0) {
        return {0, 0, 0, 0};
    }

    pref[0] = tab[0];
    maxpref[0] = max(tab[0], 0ll);
    for (ll i = 1; i < n; i++) {
        pref[i] = pref[i-1] + tab[i];
        maxpref[i] = max(maxpref[i-1], pref[i]);
    }

    post[n-1] = tab[n-1];
    maxpost[n-1] = max(tab[n-1], 0ll);
    for (ll i = n - 2; i >= 0; i--) {
        post[i] = post[i+1] + tab[i];
        maxpost[i] = max(maxpost[i+1], post[i]);
    }
    ll maxmid = post[0];
    for (ll i = 1; i < n; i++) {
        maxmid = max(maxmid, maxpref[i-1] + maxpost[i]);
    }
    return {
        post[0],
        maxmid,
        maxpref[n-1],
        maxpost[0],
    };
}

ll finish(vector<part_solution> tab) {
    ll n = tab.size();
    ll ans = 0;
    vector<ll> pref(n), post(n), maxpref(n), maxpost(n);
    
    pref[0] = tab[0].sum;
    maxpref[0] = tab[0].pref;
    for (int i = 1; i < n; i++) {
        pref[i] = pref[i - 1] + tab[i].sum;
        maxpref[i] = max(maxpref[i-1], pref[i-1] + tab[i].pref);
    }
    
    post[n-1] = tab[n-1].sum;
    maxpost[n-1] = tab[n-1].post;
    for (int i = n - 2; i >= 0; i--) {
        post[i] = post[i + 1] + tab[i].sum;
        maxpost[i] = max(maxpost[i + 1], post[i + 1] + tab[i].post);
    }

    for (int i = 0; i < n; i++) {
        // mid
        ll left = (i == 0) ? 0 : pref[i-1];
        ll right = (i == n-1) ? 0 : post[i+1];
        ans = max(ans, left + right + tab[i].mid);

        ll mleft = (i == 0) ? 0 : maxpref[i-1];
        ll mright = maxpost[i];
        ans = max(ans, mleft + mright);
    }
    ans = max(ans, maxpref[n-1]);
    return ans;
}

ll master_job() {
    int me = MyNodeId();
    int node_count = NumberOfNodes();

    int total_n = GetN();
    int worker_count = node_count - 1;
    vector<task> tasks(worker_count);
    vector<part_solution> parts(worker_count);

    ll jump = total_n / worker_count;
    ll it = 0;
    for (int i = 1; i < node_count; i++) {
        ll next = (i == node_count - 1) ? total_n : it + jump;
        task t = {it, next};
        it = next;
        tasks[i - 1] = t;
        // PutTask(i, tasks[i]);
        PutTask(i, t);
        Send(i);
    }
    for (int i = 1; i < node_count; i++) {
        Receive(i);
        auto s = GetPart(i);
        auto t = tasks[i - 1];
        parts[i - 1] = s;
        // free(&tasks[i]);
        // printf("Solution %d [%lld, %lld) - ", i, t.start, t.end);
        // prints(s);
    }
    return finish(parts);
}

void worker_job() {
    int me = MyNodeId();

    Receive(0);
    task t = GetTask(0);
    ll size = t.end - t.start;  
    vector<ll> tab(size);
    // tab.resize(size);
    for (ll i = 0; i < size; i++) {
        tab[i] = GetTaste(t.start + i);
    }
    auto s = solve(tab);
    PutPart(0, s);
    Send(0);
}

int main() {
    if (MyNodeId() == 0) {
        ll res = master_job();
        cout << res << "\n";
    } else {
        worker_job();
    }
    return 0;
}