#include <bits/stdc++.h> using namespace std; #define debug if(0) #define err stderr #define pb push_back #define mp make_pair #define ff first #define ss second #define MAXN 500005 #define ZAK 1048580 typedef long long int ll; int n; ll tab[MAXN], dp[MAXN], mini, maxi, q, R; ll INF = (ll)(1e18) + 5; struct Node; typedef struct Node* Tree; Tree tree = NULL; struct Node { ll value; Tree left, right; }; void insert(Tree* tree, ll pp, ll pk, ll gdzie, ll war) { if (*tree == NULL) { Tree nowy = (Node*) malloc(sizeof(struct Node)); nowy->left = nowy->right = NULL; nowy->value = INF; *tree = nowy; } (*tree)->value = min((*tree)->value, war); if (pp == pk) { return; } ll sr = (pp + pk) / 2; if (gdzie <= sr) { insert(&(*tree)->left, pp, sr, gdzie, war); } else { insert(&(*tree)->right, sr + 1, pk, gdzie, war); } } ll query(Tree* tree, ll pp, ll pk, ll zp, ll zk) { if (*tree == NULL) { return INF; } if (pk < zp || zk < pp) { return INF; } if (zp <= pp && pk <= zk) { return (*tree)->value; } ll sr = (pp + pk) / 2; return min(query(&(*tree)->left, pp, sr, zp, zk), query(&(*tree)->right, sr + 1, pk, zp, zk)); } void aktualizuj(int ind, ll dp) { insert(&tree, 1, R, tab[ind] + q, dp - (ind + 1)); } int main() { ios_base::sync_with_stdio(0); cin.tie(0); tree = (Node*) malloc(sizeof(struct Node)); tree->value = INF; cin >> n; for (int i = 1; i <= n; ++i) { cin >> tab[i]; tab[i] += tab[i - 1]; mini = min(mini, tab[i]); maxi = max(maxi, tab[i]); } if (tab[n] < 0) { puts ("-1"); return 0; } R = 1; while (R < (maxi - mini + 1)) { R <<= 1; } q = (1 - mini); debug { printf ("R=%lld q=%lld\n", R, q); } aktualizuj(0, 0); for (int i = 1; i <= n; ++i) { dp[i] = query(&tree, 1, R, 1, tab[i] + q); dp[i] += i; if (dp[i] >= n) { dp[i] = INF; } debug { fprintf(err, "i=%d dp[i]=%lld\n", i, dp[i]); } aktualizuj(i, dp[i]); } printf ("%lld\n", dp[n]); return 0; } /* 17 2 -5 0 2 0 0 0 4 0 0 -1 4 0 0 0 0 -3 */
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 | #include <bits/stdc++.h> using namespace std; #define debug if(0) #define err stderr #define pb push_back #define mp make_pair #define ff first #define ss second #define MAXN 500005 #define ZAK 1048580 typedef long long int ll; int n; ll tab[MAXN], dp[MAXN], mini, maxi, q, R; ll INF = (ll)(1e18) + 5; struct Node; typedef struct Node* Tree; Tree tree = NULL; struct Node { ll value; Tree left, right; }; void insert(Tree* tree, ll pp, ll pk, ll gdzie, ll war) { if (*tree == NULL) { Tree nowy = (Node*) malloc(sizeof(struct Node)); nowy->left = nowy->right = NULL; nowy->value = INF; *tree = nowy; } (*tree)->value = min((*tree)->value, war); if (pp == pk) { return; } ll sr = (pp + pk) / 2; if (gdzie <= sr) { insert(&(*tree)->left, pp, sr, gdzie, war); } else { insert(&(*tree)->right, sr + 1, pk, gdzie, war); } } ll query(Tree* tree, ll pp, ll pk, ll zp, ll zk) { if (*tree == NULL) { return INF; } if (pk < zp || zk < pp) { return INF; } if (zp <= pp && pk <= zk) { return (*tree)->value; } ll sr = (pp + pk) / 2; return min(query(&(*tree)->left, pp, sr, zp, zk), query(&(*tree)->right, sr + 1, pk, zp, zk)); } void aktualizuj(int ind, ll dp) { insert(&tree, 1, R, tab[ind] + q, dp - (ind + 1)); } int main() { ios_base::sync_with_stdio(0); cin.tie(0); tree = (Node*) malloc(sizeof(struct Node)); tree->value = INF; cin >> n; for (int i = 1; i <= n; ++i) { cin >> tab[i]; tab[i] += tab[i - 1]; mini = min(mini, tab[i]); maxi = max(maxi, tab[i]); } if (tab[n] < 0) { puts ("-1"); return 0; } R = 1; while (R < (maxi - mini + 1)) { R <<= 1; } q = (1 - mini); debug { printf ("R=%lld q=%lld\n", R, q); } aktualizuj(0, 0); for (int i = 1; i <= n; ++i) { dp[i] = query(&tree, 1, R, 1, tab[i] + q); dp[i] += i; if (dp[i] >= n) { dp[i] = INF; } debug { fprintf(err, "i=%d dp[i]=%lld\n", i, dp[i]); } aktualizuj(i, dp[i]); } printf ("%lld\n", dp[n]); return 0; } /* 17 2 -5 0 2 0 0 0 4 0 0 -1 4 0 0 0 0 -3 */ |