#include <bits/stdc++.h> using namespace std; typedef long long ll; #include "kanapka.h" #include "message.h" tuple<ll, ll, ll, ll> node_compute(ll b, ll e) { ll a, sum = 0, d = 0, best_mid = 0, best_left = 0, best_right = 0; for(ll i = b; i <= e; i++) { a = GetTaste(i); sum += a; best_left = min(best_left, sum); d = min(d + a, 0LL); best_mid = min(best_mid, d); } sum = 0; for(ll i = e; i >= b; i--) { a = GetTaste(i); sum += a; best_right = min(best_right, sum); } return make_tuple(sum, best_left, best_right, best_mid); } ll compute_all(ll n, ll s, ll l, ll r, ll m) { vector<ll> sum(n), bl(n), br(n); sum[0] = s; bl[0] = l; br[0] = r; ll cnt = 1, best = m, suma = s; while(cnt < n) { ll id = Receive(-1); sum[id] = GetLL(id); suma += sum[id]; bl[id] = GetLL(id); br[id] = GetLL(id); best = min(best, GetLL(id)); cnt++; } for(ll b = 0; b < n - 1; b++) { ll x = br[b]; for(ll e = b + 1; e < n; e++) { best = min(best, x + bl[e]); x += sum[e]; } } return suma - best; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); ll n = GetN(); ll num_nodes = NumberOfNodes(); ll node_id = MyNodeId(); ll part_size = (n + (num_nodes - n%num_nodes)) / num_nodes; ll used_nodes = (n + part_size - 1) / part_size; if(node_id >= used_nodes) return 0; ll sum, bl, br, bm; tie(sum, bl, br, bm) = node_compute(node_id * part_size, min(n - 1, (node_id + 1) * part_size - 1)); if(node_id != 0) { PutLL(0, sum); PutLL(0, bl); PutLL(0, br); PutLL(0, bm); Send(0); } else { cout << compute_all(used_nodes, sum, bl, br, bm) << endl; } }
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 | #include <bits/stdc++.h> using namespace std; typedef long long ll; #include "kanapka.h" #include "message.h" tuple<ll, ll, ll, ll> node_compute(ll b, ll e) { ll a, sum = 0, d = 0, best_mid = 0, best_left = 0, best_right = 0; for(ll i = b; i <= e; i++) { a = GetTaste(i); sum += a; best_left = min(best_left, sum); d = min(d + a, 0LL); best_mid = min(best_mid, d); } sum = 0; for(ll i = e; i >= b; i--) { a = GetTaste(i); sum += a; best_right = min(best_right, sum); } return make_tuple(sum, best_left, best_right, best_mid); } ll compute_all(ll n, ll s, ll l, ll r, ll m) { vector<ll> sum(n), bl(n), br(n); sum[0] = s; bl[0] = l; br[0] = r; ll cnt = 1, best = m, suma = s; while(cnt < n) { ll id = Receive(-1); sum[id] = GetLL(id); suma += sum[id]; bl[id] = GetLL(id); br[id] = GetLL(id); best = min(best, GetLL(id)); cnt++; } for(ll b = 0; b < n - 1; b++) { ll x = br[b]; for(ll e = b + 1; e < n; e++) { best = min(best, x + bl[e]); x += sum[e]; } } return suma - best; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); ll n = GetN(); ll num_nodes = NumberOfNodes(); ll node_id = MyNodeId(); ll part_size = (n + (num_nodes - n%num_nodes)) / num_nodes; ll used_nodes = (n + part_size - 1) / part_size; if(node_id >= used_nodes) return 0; ll sum, bl, br, bm; tie(sum, bl, br, bm) = node_compute(node_id * part_size, min(n - 1, (node_id + 1) * part_size - 1)); if(node_id != 0) { PutLL(0, sum); PutLL(0, bl); PutLL(0, br); PutLL(0, bm); Send(0); } else { cout << compute_all(used_nodes, sum, bl, br, bm) << endl; } } |