#pragma GCC optimize ("Ofast") #define _USE_MATH_DEFINES #include <bits/stdc++.h> #define FOR(i, a, b) for (int i=(a); i<(b); i++) #define FORD(i, a, b) for (int i=(a); i>(b); i--) #define PPC(x) __builtin_popcount(x) #define MSB(x) (63 - __builtin_clzll(x)) #define SZ(x) ((int)(x).size()) #define HASK(S, x) (S.find(x) != S.end()) #define pb push_back #define ALL(x) (x).begin(), (x).end() #define ithBit(m, i) ((m) >> (i) & 1) #define ft first #define sd second #define kw(a) ((a) * (a)) #ifdef DEBUG #include "debug.h" #else #define dbg(...) 0 #endif using namespace std; template <typename T1, typename T2> inline void remin(T1& a, T2 b) { a = min(a, (T1)b); } const int maxN = 1 << 19, maxTS = maxN * 2; const long long INF = 30000000000000000ll; template <typename Tp> struct Tree_t { int offset, qbegin, qend; Tp T[maxTS], res; void qpriv(int v, int left, int right) { if (left >= qend or right <= qbegin) return; if (left >= qbegin and right <= qend) { remin(res, T[v]); return; } qpriv(v * 2, left, (left + right) / 2); qpriv(v*2+1, (left + right) / 2, right); } void init(int n) { for (offset = 1; offset < n; offset *= 2) ; fill(T+1, T+offset*2, INF); } void updt(int v, long long x) { for (v += offset; v != 0; v /= 2) remin(T[v], x); } Tp query(int a, int b) { qbegin = a + offset; qend = b + offset; res = INF; qpriv(1, offset, offset * 2); return res; } }; typedef Tree_t <long long> Tree; Tree tree; long long pref[maxN], dp[maxN]; vector <int> inds; bool cmp(int i, int j) { if (pref[i] == pref[j]) return i < j; else return pref[i] < pref[j]; } void solve() { int n; scanf ("%d", &n); FOR(i, 1, n+1) scanf ("%lld", pref+i), pref[i] += pref[i-1]; inds.resize(n+1); iota(ALL(inds), 0); sort(ALL(inds), cmp); tree.init(n+7); for (int i : inds) { dp[i] = tree.query(0, i) + i-1; if (i == 0) dp[i] = 0; tree.updt(i, dp[i]-i); } if (dp[n] > n) dp[n] = -1ll; printf("%lld\n", dp[n]); } int main() { int t = 1; //scanf ("%d", &t); FOR(tid, 1, t+1) { //printf("Case #%d: ", tid); solve(); } 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 | #pragma GCC optimize ("Ofast") #define _USE_MATH_DEFINES #include <bits/stdc++.h> #define FOR(i, a, b) for (int i=(a); i<(b); i++) #define FORD(i, a, b) for (int i=(a); i>(b); i--) #define PPC(x) __builtin_popcount(x) #define MSB(x) (63 - __builtin_clzll(x)) #define SZ(x) ((int)(x).size()) #define HASK(S, x) (S.find(x) != S.end()) #define pb push_back #define ALL(x) (x).begin(), (x).end() #define ithBit(m, i) ((m) >> (i) & 1) #define ft first #define sd second #define kw(a) ((a) * (a)) #ifdef DEBUG #include "debug.h" #else #define dbg(...) 0 #endif using namespace std; template <typename T1, typename T2> inline void remin(T1& a, T2 b) { a = min(a, (T1)b); } const int maxN = 1 << 19, maxTS = maxN * 2; const long long INF = 30000000000000000ll; template <typename Tp> struct Tree_t { int offset, qbegin, qend; Tp T[maxTS], res; void qpriv(int v, int left, int right) { if (left >= qend or right <= qbegin) return; if (left >= qbegin and right <= qend) { remin(res, T[v]); return; } qpriv(v * 2, left, (left + right) / 2); qpriv(v*2+1, (left + right) / 2, right); } void init(int n) { for (offset = 1; offset < n; offset *= 2) ; fill(T+1, T+offset*2, INF); } void updt(int v, long long x) { for (v += offset; v != 0; v /= 2) remin(T[v], x); } Tp query(int a, int b) { qbegin = a + offset; qend = b + offset; res = INF; qpriv(1, offset, offset * 2); return res; } }; typedef Tree_t <long long> Tree; Tree tree; long long pref[maxN], dp[maxN]; vector <int> inds; bool cmp(int i, int j) { if (pref[i] == pref[j]) return i < j; else return pref[i] < pref[j]; } void solve() { int n; scanf ("%d", &n); FOR(i, 1, n+1) scanf ("%lld", pref+i), pref[i] += pref[i-1]; inds.resize(n+1); iota(ALL(inds), 0); sort(ALL(inds), cmp); tree.init(n+7); for (int i : inds) { dp[i] = tree.query(0, i) + i-1; if (i == 0) dp[i] = 0; tree.updt(i, dp[i]-i); } if (dp[n] > n) dp[n] = -1ll; printf("%lld\n", dp[n]); } int main() { int t = 1; //scanf ("%d", &t); FOR(tid, 1, t+1) { //printf("Case #%d: ", tid); solve(); } return 0; } |