#include <cstdio> #include <cassert> #include <algorithm> #include <functional> #include <unordered_map> using namespace std; int n; int j; typedef struct { long long val; long long cost; long long sumval; long long sumcost; } node; unordered_map<std::string,node> m; const int max_n = 500001; long len[max_n]; void print_len() { for(int i = 1; i < j; ++i) { printf("(%d), (%ld)\n", i, len[i]); } printf("\n"); } string make_key(int a, int b) { char s[25]; sprintf(s, "%d %d", a, b); return string(s); } void print_map() { for (auto& x: m) { printf("%s - val %lld, cost %lld, sumval %lld, sumcost %lld\n", x.first.c_str(), x.second.val, x.second.cost, x.second.sumval, x.second.sumcost); } } void print_node(node nd) { printf("val %lld, cost %lld, sumval %lld, sumcost %lld\n", nd.val, nd.cost, nd.sumval, nd.sumcost); } node calc(int a, int b) { node nd1, nd2; bool found = false; node best; best.cost = -1; for(int i = a; i < b; ++i) { auto it = m.find( make_key(a, i) ); if ( it == m.end() ) { nd1 = calc(a, i); m.insert( pair<string,node>(make_key(a, i), nd1) ); } else { nd1 = it->second; } it = m.find( make_key(i+1, b) ); if ( it == m.end() ) { nd2 = calc(i+1, b); m.insert( pair<string,node>(make_key(i+1, b), nd2) ); } else { nd2 = it->second; } if (nd1.val >= 0 && nd2.val >= 0) { found = true; if (best.cost == -1 || best.cost > nd1.cost + nd2.cost) { best.cost = nd1.cost + nd2.cost; best.val = nd1.val + nd2.val; } } } best.sumval = nd1.sumval + nd2.sumval; best.sumcost = nd1.sumcost + len[b-1]; if (!found) { best.cost = best.sumcost; best.val = best.sumval; } return best; } int main() { node nd; int prev=1; long k; long long sumval=0; bool is_factory = false; scanf("%d\n", &n); nd.cost = 0; nd.sumcost = 0; j = 0; for(int i=1; i <= n; ++i) { scanf("%ld", &k); if (k == 0) { prev++; continue; } j++; if (j > 1) { len[j-1] = prev; } prev = 1; nd.val = k; nd.sumval = k; sumval += k; if (k < 0) { is_factory = true; } m.insert( pair<string,node>(make_key(j, j), nd) ); } if (sumval < 0) { printf("-1\n"); return 0; } // print_len(); // print_map(); nd = calc(1, j); // print_map(); // print_node(nd); if (nd.cost > 0) { printf("%lld\n", nd.cost); } else { if (is_factory) { printf("-1\n"); } else { printf("0\n"); } } 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 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 | #include <cstdio> #include <cassert> #include <algorithm> #include <functional> #include <unordered_map> using namespace std; int n; int j; typedef struct { long long val; long long cost; long long sumval; long long sumcost; } node; unordered_map<std::string,node> m; const int max_n = 500001; long len[max_n]; void print_len() { for(int i = 1; i < j; ++i) { printf("(%d), (%ld)\n", i, len[i]); } printf("\n"); } string make_key(int a, int b) { char s[25]; sprintf(s, "%d %d", a, b); return string(s); } void print_map() { for (auto& x: m) { printf("%s - val %lld, cost %lld, sumval %lld, sumcost %lld\n", x.first.c_str(), x.second.val, x.second.cost, x.second.sumval, x.second.sumcost); } } void print_node(node nd) { printf("val %lld, cost %lld, sumval %lld, sumcost %lld\n", nd.val, nd.cost, nd.sumval, nd.sumcost); } node calc(int a, int b) { node nd1, nd2; bool found = false; node best; best.cost = -1; for(int i = a; i < b; ++i) { auto it = m.find( make_key(a, i) ); if ( it == m.end() ) { nd1 = calc(a, i); m.insert( pair<string,node>(make_key(a, i), nd1) ); } else { nd1 = it->second; } it = m.find( make_key(i+1, b) ); if ( it == m.end() ) { nd2 = calc(i+1, b); m.insert( pair<string,node>(make_key(i+1, b), nd2) ); } else { nd2 = it->second; } if (nd1.val >= 0 && nd2.val >= 0) { found = true; if (best.cost == -1 || best.cost > nd1.cost + nd2.cost) { best.cost = nd1.cost + nd2.cost; best.val = nd1.val + nd2.val; } } } best.sumval = nd1.sumval + nd2.sumval; best.sumcost = nd1.sumcost + len[b-1]; if (!found) { best.cost = best.sumcost; best.val = best.sumval; } return best; } int main() { node nd; int prev=1; long k; long long sumval=0; bool is_factory = false; scanf("%d\n", &n); nd.cost = 0; nd.sumcost = 0; j = 0; for(int i=1; i <= n; ++i) { scanf("%ld", &k); if (k == 0) { prev++; continue; } j++; if (j > 1) { len[j-1] = prev; } prev = 1; nd.val = k; nd.sumval = k; sumval += k; if (k < 0) { is_factory = true; } m.insert( pair<string,node>(make_key(j, j), nd) ); } if (sumval < 0) { printf("-1\n"); return 0; } // print_len(); // print_map(); nd = calc(1, j); // print_map(); // print_node(nd); if (nd.cost > 0) { printf("%lld\n", nd.cost); } else { if (is_factory) { printf("-1\n"); } else { printf("0\n"); } } return 0; } |