#include "kanapka.h" #include "message.h" #include <iostream> #include <algorithm> using namespace std; typedef long long int ll; int N; struct Node { ll sum; ll max_pref; ll max_suf; Node(){} Node(ll sum, ll max_pref, ll max_suf) { this->sum = sum; this->max_pref = max_pref; this->max_suf = max_suf; } }; void count_my(ll &sum, ll & max_pref, ll & max_suf) { int start = (MyNodeId() * N) / NumberOfNodes(); int end = ((MyNodeId() + 1) * N) / NumberOfNodes(); //cout << MyNodeId() << ": " << start << " - " << end << endl; max_pref = 0; for(int i = start; i < end; ++i) { sum += GetTaste(i); max_pref = max(max_pref, sum); } max_suf = 0; ll temp = 0; for(int i = end-1; i >= start; --i) { temp += GetTaste(i); max_suf = max(max_suf, temp); } } ll get_range(ll t[], int a, int b) { if(a > b) return 0; ll ret = t[b]; return (a > 0)? ret-t[a-1] : ret; } ll count_ans(Node t[], int lenght) { for(int i = 1; i < lenght; ++i) { int index = Receive(-1); ll sum = GetLL(index); ll max_pref = GetLL(index); ll max_suf = GetLL(index); t[index] = Node(sum, max_pref, max_suf); } ll ret = 0; ll *t_sum = new ll[lenght]; t_sum[0] = t[0].sum; for(int i = 1; i < lenght; ++i) { t_sum[i] = t_sum[i-1] + t[i].sum; } for(int i = 0; i < lenght; ++i) { for(int j = i; j < lenght; ++j) { ll left_sum = get_range(t_sum, 0,i-1); ll right_sum = get_range(t_sum, j+1, lenght-1); ll part = t[i].max_pref; if(i == j) { part = max(part, t[i].max_suf); } else { part += t[j].max_suf; } ret = max(ret, left_sum + right_sum + part); } } return ret; } int main() { ios_base::sync_with_stdio(false); N = GetN(); ll sum = 0; ll max_pref = 0; ll max_suf = 0; count_my(sum, max_pref, max_suf); if(MyNodeId() > 0) { PutLL(0, sum); PutLL(0, max_pref); PutLL(0, max_suf); Send(0); } else { Node *t = new Node[NumberOfNodes()]; t[0] = Node(sum, max_pref, max_suf); cout << count_ans(t, NumberOfNodes()) << endl; } return 0; }
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 | #include "kanapka.h" #include "message.h" #include <iostream> #include <algorithm> using namespace std; typedef long long int ll; int N; struct Node { ll sum; ll max_pref; ll max_suf; Node(){} Node(ll sum, ll max_pref, ll max_suf) { this->sum = sum; this->max_pref = max_pref; this->max_suf = max_suf; } }; void count_my(ll &sum, ll & max_pref, ll & max_suf) { int start = (MyNodeId() * N) / NumberOfNodes(); int end = ((MyNodeId() + 1) * N) / NumberOfNodes(); //cout << MyNodeId() << ": " << start << " - " << end << endl; max_pref = 0; for(int i = start; i < end; ++i) { sum += GetTaste(i); max_pref = max(max_pref, sum); } max_suf = 0; ll temp = 0; for(int i = end-1; i >= start; --i) { temp += GetTaste(i); max_suf = max(max_suf, temp); } } ll get_range(ll t[], int a, int b) { if(a > b) return 0; ll ret = t[b]; return (a > 0)? ret-t[a-1] : ret; } ll count_ans(Node t[], int lenght) { for(int i = 1; i < lenght; ++i) { int index = Receive(-1); ll sum = GetLL(index); ll max_pref = GetLL(index); ll max_suf = GetLL(index); t[index] = Node(sum, max_pref, max_suf); } ll ret = 0; ll *t_sum = new ll[lenght]; t_sum[0] = t[0].sum; for(int i = 1; i < lenght; ++i) { t_sum[i] = t_sum[i-1] + t[i].sum; } for(int i = 0; i < lenght; ++i) { for(int j = i; j < lenght; ++j) { ll left_sum = get_range(t_sum, 0,i-1); ll right_sum = get_range(t_sum, j+1, lenght-1); ll part = t[i].max_pref; if(i == j) { part = max(part, t[i].max_suf); } else { part += t[j].max_suf; } ret = max(ret, left_sum + right_sum + part); } } return ret; } int main() { ios_base::sync_with_stdio(false); N = GetN(); ll sum = 0; ll max_pref = 0; ll max_suf = 0; count_my(sum, max_pref, max_suf); if(MyNodeId() > 0) { PutLL(0, sum); PutLL(0, max_pref); PutLL(0, max_suf); Send(0); } else { Node *t = new Node[NumberOfNodes()]; t[0] = Node(sum, max_pref, max_suf); cout << count_ans(t, NumberOfNodes()) << endl; } return 0; } |