text
stringlengths
49
983k
#include <bits/stdc++.h> using namespace std; class SegTree_Max { vector<int> st, st2, id; vector<int> lazy; int n; void prop(int p, int L, int R) { if (lazy[p]) { st[p] += lazy[p]; lazy[2 * p] += lazy[p]; lazy[2 * p + 1] += lazy[p]; lazy[p] = 0; } } void upd(int p, int L, int R, int i, int j, int v) { prop(p, L, R); if (j < L || i > R) { return; } if (i <= L && R <= j) { lazy[p] = v; prop(p, L, R); return; } int mid = (L + R) / 2; upd(2 * p, L, mid, i, j, v); upd(2 * p + 1, mid + 1, R, i, j, v); st[p] = max(st[2 * p], st[2 * p + 1]); } int qry(int p, int L, int R, int i, int j) { prop(p, L, R); if (j < L || i > R) { return -1e9; } if (i <= L && R <= j) { return st[p]; } int mid = (L + R) / 2; return max(qry(2 * p, L, mid, i, j), qry(2 * p + 1, mid + 1, R, i, j)); } public: SegTree_Max(int sz) { n = sz; st.assign(9 * (n + 1), 0); lazy.assign(9 * (n + 1), 0); } int qry(int i, int j) { return qry(1, 1, n, i, j); } void upd(int i, int j, int v) { upd(1, 1, n, i, j, v); } }; class SegTree_Sum { vector<int> st, st2, id; vector<int> lazy; int n; void prop(int p, int L, int R) { if (lazy[p]) { st[p] += lazy[p]; lazy[2 * p] += lazy[p]; lazy[2 * p + 1] += lazy[p]; lazy[p] = 0; } } void upd(int p, int L, int R, int i, int j, int v) { prop(p, L, R); if (j < L || i > R) return; if (i <= L && R <= j) { lazy[p] = v; prop(p, L, R); return; } int mid = (L + R) / 2; upd(2 * p, L, mid, i, j, v); upd(2 * p + 1, mid + 1, R, i, j, v); int qryLo = st[2 * p]; int qryHi = st[2 * p + 1]; st[p] = qryLo + qryHi; } int qry(int p, int L, int R, int i, int j) { prop(p, L, R); if (j < L || i > R) return 0; if (i <= L && R <= j) return st[p]; int mid = (L + R) / 2; int qryLo = qry(2 * p, L, mid, i, j); int qryHi = qry(2 * p + 1, mid + 1, R, i, j); return qryLo + qryHi; } public: SegTree_Sum(int sz) { n = sz; st.assign(9 * (n + 1), 0); lazy.assign(9 * (n + 1), 0); } int qry(int i, int j) { return qry(1, 1, n, i, j); } void upd(int i, int j, int v) { upd(1, 1, n, i, j, v); } }; int main() { ios::sync_with_stdio(false); int n, d, x, tot = 0; cin >> n >> d; SegTree_Sum st_sum(n); SegTree_Max st_max(n); vector<int> v(n + 1); for (int(i) = (1), _MAX = (n + 1); (i) < _MAX; (i)++) { cin >> x; v[i] = x; st_sum.upd(i, i, x); st_max.upd(i, i, st_sum.qry(1, i)); if (st_sum.qry(1, i) > d) { cout << -1 << endl; return 0; } } for (int(i) = (1), _MAX = (n + 1); (i) < _MAX; (i)++) { int val = st_sum.qry(1, i); if (v[i] == 0 && val < 0) { int mx = st_max.qry(i, n); mx = d - mx; if (val + mx < 0) { cout << -1 << endl; return 0; } st_sum.upd(i, i, mx); st_max.upd(i, n, mx); tot++; } } cout << tot << endl; return 0; }
#include <bits/stdc++.h> #pragma GCC target("avx2") #pragma GCC optimize("Ofast") #pragma GCC optimize("unroll-loops") using namespace std; namespace atcoder { namespace internal { int ceil_pow2(int n) { int x = 0; while ((1U << x) < (unsigned int)(n)) x++; return x; } int bsf(unsigned int n) { return __builtin_ctz(n); } } // namespace internal } // namespace atcoder namespace atcoder { template <class S, S (*op)(S, S), S (*e)(), class F, S (*mapping)(F, S), F (*composition)(F, F), F (*id)()> struct lazy_segtree { public: lazy_segtree() : lazy_segtree(0) {} lazy_segtree(int n) : lazy_segtree(std::vector<S>(n, e())) {} lazy_segtree(const std::vector<S>& v) : _n(int(v.size())) { log = internal::ceil_pow2(_n); size = 1 << log; d = std::vector<S>(2 * size, e()); lz = std::vector<F>(size, id()); for (int i = 0; i < _n; i++) d[size + i] = v[i]; for (int i = size - 1; i >= 1; i--) { update(i); } } void set(int p, S x) { assert(0 <= p && p < _n); p += size; for (int i = log; i >= 1; i--) push(p >> i); d[p] = x; for (int i = 1; i <= log; i++) update(p >> i); } S get(int p) { assert(0 <= p && p < _n); p += size; for (int i = log; i >= 1; i--) push(p >> i); return d[p]; } S prod(int l, int r) { assert(0 <= l && l <= r && r <= _n); if (l == r) return e(); l += size; r += size; for (int i = log; i >= 1; i--) { if (((l >> i) << i) != l) push(l >> i); if (((r >> i) << i) != r) push(r >> i); } S sml = e(), smr = e(); while (l < r) { if (l & 1) sml = op(sml, d[l++]); if (r & 1) smr = op(d[--r], smr); l >>= 1; r >>= 1; } return op(sml, smr); } S all_prod() { return d[1]; } void apply(int p, F f) { assert(0 <= p && p < _n); p += size; for (int i = log; i >= 1; i--) push(p >> i); d[p] = mapping(f, d[p]); for (int i = 1; i <= log; i++) update(p >> i); } void apply(int l, int r, F f) { assert(0 <= l && l <= r && r <= _n); if (l == r) return; l += size; r += size; for (int i = log; i >= 1; i--) { if (((l >> i) << i) != l) push(l >> i); if (((r >> i) << i) != r) push((r - 1) >> i); } { int l2 = l, r2 = r; while (l < r) { if (l & 1) all_apply(l++, f); if (r & 1) all_apply(--r, f); l >>= 1; r >>= 1; } l = l2; r = r2; } for (int i = 1; i <= log; i++) { if (((l >> i) << i) != l) update(l >> i); if (((r >> i) << i) != r) update((r - 1) >> i); } } template <bool (*g)(S)> int max_right(int l) { return max_right(l, [](S x) { return g(x); }); } template <class G> int max_right(int l, G g) { assert(0 <= l && l <= _n); assert(g(e())); if (l == _n) return _n; l += size; for (int i = log; i >= 1; i--) push(l >> i); S sm = e(); do { while (l % 2 == 0) l >>= 1; if (!g(op(sm, d[l]))) { while (l < size) { push(l); l = (2 * l); if (g(op(sm, d[l]))) { sm = op(sm, d[l]); l++; } } return l - size; } sm = op(sm, d[l]); l++; } while ((l & -l) != l); return _n; } template <bool (*g)(S)> int min_left(int r) { return min_left(r, [](S x) { return g(x); }); } template <class G> int min_left(int r, G g) { assert(0 <= r && r <= _n); assert(g(e())); if (r == 0) return 0; r += size; for (int i = log; i >= 1; i--) push((r - 1) >> i); S sm = e(); do { r--; while (r > 1 && (r % 2)) r >>= 1; if (!g(op(d[r], sm))) { while (r < size) { push(r); r = (2 * r + 1); if (g(op(d[r], sm))) { sm = op(d[r], sm); r--; } } return r + 1 - size; } sm = op(d[r], sm); } while ((r & -r) != r); return 0; } private: int _n, size, log; std::vector<S> d; std::vector<F> lz; void update(int k) { d[k] = op(d[2 * k], d[2 * k + 1]); } void all_apply(int k, F f) { d[k] = mapping(f, d[k]); if (k < size) lz[k] = composition(f, lz[k]); } void push(int k) { all_apply(2 * k, lz[k]); all_apply(2 * k + 1, lz[k]); lz[k] = id(); } }; } // namespace atcoder using namespace atcoder; using dou = long double; string yes = "yes"; string Yes = "Yes"; string YES = "YES"; string no = "no"; string No = "No"; string NO = "NO"; template <class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; } template <class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; } const long long mod = 1000000007; struct mint { long long x; mint(long long x = 0) : x((x % mod + mod) % mod) {} mint operator-() const { return mint(-x); } mint& operator+=(const mint a) { if ((x += a.x) >= mod) x -= mod; return *this; } mint& operator-=(const mint a) { if ((x += mod - a.x) >= mod) x -= mod; return *this; } mint& operator*=(const mint a) { (x *= a.x) %= mod; return *this; } mint operator+(const mint a) const { return mint(*this) += a; } mint operator-(const mint a) const { return mint(*this) -= a; } mint operator*(const mint a) const { return mint(*this) *= a; } mint pow(long long t) const { if (!t) return 1; mint a = pow(t >> 1); a *= a; if (t & 1) a *= *this; return a; } mint inv() const { return pow(mod - 2); } mint& operator/=(const mint a) { return *this *= a.inv(); } mint operator/(const mint a) const { return mint(*this) /= a; } }; istream& operator>>(istream& is, const mint& a) { return is >> a.x; } ostream& operator<<(ostream& os, const mint& a) { return os << a.x; } long long ceil(long long a, long long b) { return ((a + b - 1) / b); } const long long INF64 = 3223372036854775807ll; const long long MOD = 1000000007ll; const long long OD = 1000000000000007ll; const dou pi = 3.141592653589793; long long modpow(long long a, long long n) { long long resize = 1; while (n > 0) { if (n & 1) resize = resize * a % MOD; a = a * a % MOD; n >>= 1; } return resize; } using S = long long; using F = long long; const S INF = 8e18; S op(S a, S b) { return std::max(a, b); } S e() { return -INF; } S mapping(F f, S x) { return f + x; } F composition(F f, F g) { return f + g; } F id() { return 0; } int main() { int n, d; std::cin >> n >> d; std::vector<long long> a(n); for (long long i = 0; i < (long long)(n); i++) std::cin >> a[i]; ; std::vector<long long> sa(n); sa[0] = a[0]; for (long long i = 0; i < (long long)(n - 1); i++) { sa[i + 1] += sa[i] + a[i + 1]; } int s = 0; int ans = 0; for (long long i = 0; i < (long long)(n); i++) { if (sa[i] > d) { std::cout << -1 << std::endl; exit(0); } } atcoder::lazy_segtree<S, op, e, F, mapping, composition, id> seg(sa); for (long long i = 0; i < (long long)(n); i++) { if (a[i] == 0) { if (seg.get(i) < 0) { long long ne = -seg.get(i); long long ma = d - seg.prod(i, n); if (ne > ma) { std::cout << -1 << std::endl; exit(0); } seg.apply(i, n, ma); ans++; } } } std::cout << ans << std::endl; }
#include <bits/stdc++.h> using namespace std; int n, d; int x; int mi = 0, mx = 0; int ans; int main() { scanf("%d%d", &n, &d); for (int i(1); i <= (n); ++i) { scanf("%d", &x); if (x) { mi += x, mx += x; if (mi > d) return 0 * puts("-1"); mx = min(mx, d); } else { if (mx >= 0) mi = max(mi, 0); else ++ans, mx = d, mi = 0; } } printf("%d\n", ans); return 0; }
#include <bits/stdc++.h> using namespace std; template <typename T> void read(T& x) { x = 0; char c; int sign = 1; do { c = getchar(); if (c == '-') sign = -1; } while (c < '0' || c > '9'); do { x = x * 10 + c - '0'; c = getchar(); } while (c <= '9' && c >= '0'); x *= sign; } const int N = 1e5 + 500; int ans; long long n, d, ASK[N], p, sum[N], mx[N]; int main() { read(n); read(d); for (register int i = 1; i <= n; ++i) read(ASK[i]); mx[n + 1] = -1e18; for (register int i = 1; i <= n; ++i) sum[i] += sum[i - 1] + ASK[i]; for (register int i = n; i >= 1; --i) mx[i] = max(mx[i + 1], sum[i]); if (mx[1] + p > d) return puts("-1"), 0; for (register int i = 1; i <= n; ++i) { if (sum[i] + p > d) return puts("-1"), 0; if (ASK[i] == 0 && sum[i] + p < 0) { p += d - (mx[i] + p); ans++; if (sum[i] + p < 0) return puts("-1"), 0; } } printf("%d", ans); return 0; }
#include <bits/stdc++.h> using namespace std; long long a[100005], n; bool t[100005]; long long sparse1[100005][20], sparse2[100005][20]; void built() { for (long long i = 1; i <= n; i++) { sparse1[i][0] = a[i]; sparse2[i][0] = a[i]; } for (long long j = 1; j < 20; j++) { for (long long i = 1; i <= n - (1 << j) + 1; i++) { sparse1[i][j] = max(sparse1[i][j - 1], sparse1[i + (1 << (j - 1))][j - 1]); sparse2[i][j] = min(sparse2[i][j - 1], sparse2[i + (1 << (j - 1))][j - 1]); } } } long long getmax(long long l, long long r) { long long ans = INT_MIN; for (long long j = 19; j >= 0; j--) { if (l + (1 << j) - 1 <= r) { ans = max(ans, sparse1[l][j]); l += (1 << j); } } return ans; } long long getmin(long long l, long long r) { long long ans = INT_MAX; for (long long j = 19; j >= 0; j--) { if (l + (1 << j) - 1 <= r) { ans = min(ans, sparse2[l][j]); l += (1 << j); } } return ans; } int32_t main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); long long d = 0, l = 1, k, no = 0; cin >> n >> k; for (long long i = 1; i <= n; i++) { cin >> a[i]; t[i] = (a[i] == 0); a[i] += a[i - 1]; } built(); for (long long i = 1; i <= n; i++) { if (d + a[i] > k) return cout << -1, 0; if (t[i] && a[i] + d < 0) { if (no == 0 || -a[i] + getmax(l, i) > k) l = i; else if (-a[i] + getmax(l, i) <= k) no--; no++; d = -a[i]; } } cout << no; }
#include <bits/stdc++.h> using namespace std; int setBit(int n, int pos) { return n = n | (1 << pos); } int resetBit(int n, int pos) { return n = n & ~(1 << pos); } bool checkBit(int n, int pos) { return (bool)(n & (1 << pos)); } const int MAX = 1e5 + 10; struct node { int mn, mx; } tree[4 * MAX]; int ara[MAX], cum[MAX]; vector<int> ck; int lazy[4 * MAX]; node Merge(node a, node b) { node ret; ret.mn = min(a.mn, b.mn); ret.mx = max(a.mx, b.mx); return ret; } void lazyUpdate(int node, int st, int ed) { if (lazy[node]) { tree[node].mn += lazy[node]; tree[node].mx += lazy[node]; if (st != ed) { int left = node << 1, right = left | 1; lazy[left] += lazy[node]; lazy[right] += lazy[node]; } lazy[node] = 0; } } void build(int node, int st, int ed) { lazy[node] = 0; int left = node << 1, right = left | 1, mid = (st + ed) >> 1; if (st == ed) { tree[node].mn = tree[node].mx = cum[st]; return; } build(left, st, mid); build(right, mid + 1, ed); tree[node] = Merge(tree[left], tree[right]); } void update(int node, int st, int ed, int i, int j, int v) { int left = node << 1, right = left | 1, mid = (st + ed) >> 1; lazyUpdate(node, st, ed); if (st > j || ed < i) return; if (st >= i && ed <= j) { lazy[node] += v; lazyUpdate(node, st, ed); return; } update(left, st, mid, i, j, v); update(right, mid + 1, ed, i, j, v); tree[node] = Merge(tree[left], tree[right]); } node query(int node, int st, int ed, int i, int j) { lazyUpdate(node, st, ed); int left = node << 1, right = left | 1, mid = (st + ed) >> 1; if (st >= i && ed <= j) return tree[node]; if (i > mid) return query(right, mid + 1, ed, i, j); else if (j <= mid) return query(left, st, mid, i, j); else return Merge(query(right, mid + 1, ed, i, j), query(left, st, mid, i, j)); } int main() { int n, d, i; scanf("%d %d", &n, &d); cum[0] = 0; for (i = 1; i <= n; i++) { scanf("%d", &ara[i]); cum[i] = cum[i - 1] + ara[i]; if (ara[i] == 0) ck.push_back(i); } build(1, 1, n); int last = 1, cnt = 0; node now, lala; int y, x; for (i = 0; i < ck.size(); i++) { x = ck[i]; now = query(1, 1, n, x, x); if (now.mn < 0) { lala = query(1, 1, n, x, n); y = d - lala.mx; if (y + now.mn >= 0) { update(1, 1, n, x, n, y); cnt++; } else { puts("-1"); return 0; } } } now = query(1, 1, n, 1, n); if (now.mx > d) { printf("-1\n"); } else printf("%d\n", cnt); return 0; }
#include <bits/stdc++.h> using namespace std; template <typename T> void _R(T &x) { cin >> x; } template <typename T> void _R(vector<T> &x) { for (auto it = x.begin(); it != x.end(); it++) { _R(*it); } } void R() {} template <typename T, typename... K> void R(T &head, K &...tail) { _R(head); R(tail...); } template <typename T> void _W(const T &x, const char c) { cout << x; } template <typename T> void _W(const vector<T> &x, const char c) { for (auto it = x.cbegin(); it != x.cend(); it++) { if (it != x.cbegin()) putchar(c); _W(*it, c); } } void W() {} template <typename T, typename... K> void W(const T &head, const K &...tail) { _W(head, ' '); cout << (sizeof...(tail) ? ' ' : '\n') << flush; W(tail...); } const double PI = 3.14159265358979323846264338327950288419716939937510582097494459230; template <typename T> inline T gcd(T a, T b) { if (a == 0) return b; return gcd(b % a, a); } template <typename T> inline void amin(T &x, T y) { if (x > y) x = y; } template <typename T> inline void amax(T &x, T y) { if (x < y) x = y; } template <typename A, typename B> class comp { public: bool operator()(const pair<A, B> &a, const pair<A, B> &b) { if (a.first != b.first) return a.first < b.first; else return a.first > b.first; } }; long long fast_exp(long long a, long long b) { long long res = 1; while (b) { if (b & 1LL) { res *= a; res %= 1000000007; } b >>= 1LL; a *= a; a %= 1000000007; } return res; } int n, d; vector<int> a, S; vector<long long> st, lazy; void preprocess(void) { return; } void build(int v, int l, int r) { if (l > r) { return; } if (l == r) { st[v] = S[l]; return; } int mid = (l + r) >> 1; build(((v) << 1), l, mid); build((((v) << 1) + 1), mid + 1, r); st[v] = max(st[((v) << 1)], st[(((v) << 1) + 1)]); } void update(int v, int ql, int qr, int l, int r, long long diff) { if (lazy[v] != 0) { st[v] = st[v] + lazy[v]; if (l != r) { lazy[((v) << 1)] += lazy[v]; lazy[(((v) << 1) + 1)] += lazy[v]; } lazy[v] = 0; } if (l > qr || r < ql) { return; } if (l >= ql && r <= qr) { st[v] += diff; if (l != r) { lazy[((v) << 1)] += diff; lazy[(((v) << 1) + 1)] += diff; } return; } int mid = (l + r) >> 1; update(((v) << 1), ql, qr, l, mid, diff); update((((v) << 1) + 1), ql, qr, mid + 1, r, diff); st[v] = max(st[((v) << 1)], st[(((v) << 1) + 1)]); } long long query(int v, int ql, int qr, int l, int r) { if (lazy[v] != 0) { st[v] = st[v] + lazy[v]; if (l != r) { lazy[((v) << 1)] += lazy[v]; lazy[(((v) << 1) + 1)] += lazy[v]; } lazy[v] = 0; } if (l > qr || r < ql) { return -1000000007; } if (l >= ql && r <= qr) { return st[v]; } int mid = (l + r) >> 1; return max(query(((v) << 1), ql, qr, l, mid), query((((v) << 1) + 1), ql, qr, mid + 1, r)); } int main(void) { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.precision(20); preprocess(); int teeee; teeee = 1; for (int zeeee = (1); zeeee <= (teeee); zeeee++) { R(n, d); a.resize(n), S.resize(n + 1); R(a); S[0] = 0; for (int i = (1); i <= (n); i++) { S[i] = S[i - 1] + a[i - 1]; }; for (int i = (0); i <= ((int)(n)-1); i++) { if (S[i + 1] > d) { W(-1); exit(0); } } st.resize(6 * n + 4); lazy.assign(6 * n + 4, 0); build(1, 1, n); int cnt = 0; for (int i = (1); i <= (n); i++) { if (a[i - 1] == 0) { long long q1 = query(1, i, i, 1, n); ; if (q1 < 0) { long long q = query(1, i, n, 1, n); ; if (q > d) { W(-1); exit(0); } if (q - q1 > d) { W(-1); exit(0); } cnt++; update(1, i, n, 1, n, d - (q)); } } } W(cnt); } cerr << (clock() / (double)CLOCKS_PER_SEC) << endl; return 0; }
#include <bits/stdc++.h> using namespace std; int a[100010]; int n, d, min1 = 0, max1 = 0, ans = 0; int main() { scanf("%d %d", &n, &d); for (int i = 1; i <= n; i++) { scanf("%d", &a[i]); } for (int i = 1; i <= n; i++) { if (!a[i]) { if (min1 < 0) { min1 = 0; } if (max1 < 0) { max1 = d; ans++; } } else { min1 += a[i]; max1 += a[i]; if (min1 > d) { printf("-1\n"); return 0; } if (max1 > d) { max1 = d; } } } printf("%d\n", ans); return 0; }
#include <bits/stdc++.h> using namespace std; const long long INF = 1e18; long long n, d, a[100005], sum[100005], maxm[100005], now, ans; int main() { while (~scanf("%lld %lld", &n, &d)) { maxm[n + 1] = -1e18; for (long long i = 1; i <= n; i++) { maxm[i] = -1e18; scanf("%lld", &a[i]); sum[i] = sum[i - 1] + a[i]; } for (long long i = n; i >= 1; i--) { maxm[i] = max(maxm[i + 1], sum[i]); } if (maxm[1] > d) { printf("-1\n"); return 0; } for (long long i = 1; i <= n; i++) { if (a[i] == 0) { if (now + sum[i] < 0) { long long M = maxm[i]; long long c = d - (M + now); if (c <= 0) { printf("-1\n"); return 0; } else { now += c; ans++; } } if (now + sum[i] < 0) { printf("-1\n"); return 0; } } } printf("%lld\n", ans); } }
#include <bits/stdc++.h> using namespace std; void exitall() { cout << -1; exit(0); } int main() { ios::sync_with_stdio(false); int n; long long d; cin >> n >> d; vector<long long> v(n); for (int i = 0; i < n; ++i) { cin >> v[i]; } long long mx = 0; int ans = 0; int lst = -1; long long b = 0; for (int i = 0; i < n; ++i) { if (v[i] == 0) { if (lst >= 0) { if (b >= 0) continue; if (-b <= d - mx) { mx -= b; b = 0; } else { ans++; b = 0; lst = i; mx = 0; } } else { if (b < 0) { ans++; b = 0; mx = 0; lst = i; } } } else mx = max(mx, b = b + v[i]); if (b > d) { cout << -1; return 0; } } cout << ans; return 0; }
#include <bits/stdc++.h> using namespace std; template <class T> inline T bigmod(T p, T e, T M) { long long int ret = 1; for (; e > 0; e >>= 1) { if (e & 1) ret = (ret * p) % M; p = (p * p) % M; } return (T)ret; } template <class T> inline T gcd(T a, T b) { if (b == 0) return a; return gcd(b, a % b); } template <class T> inline T modinverse(T a, T M) { return bigmod(a, M - 2, M); } template <class T> inline T bpow(T p, T e) { long long int ret = 1; for (; e > 0; e >>= 1) { if (e & 1) ret = (ret * p); p = (p * p); } return (T)ret; } int toInt(string s) { int sm; stringstream ss(s); ss >> sm; return sm; } int toLlint(string s) { long long int sm; stringstream ss(s); ss >> sm; return sm; } int ts, kk = 1; int n, m; int a[100005]; long long int cr[100005]; long long int mx[100005]; int main() { int t, i, j, k; scanf("%d%d", &n, &m); for (i = 0; i < n; i++) { scanf("%d", &a[i]); cr[i] = a[i]; if (i) cr[i] += cr[i - 1]; if (cr[i] > m) { printf("-1\n"); return 0; } } mx[n - 1] = cr[n - 1]; for (i = n - 2; i > -1; i--) { mx[i] = max(cr[i], mx[i + 1]); } int rs = 0; long long int nw = 0, bs = 0; for (i = 0; i < n; i++) { if (a[i] == 0) { if (nw < 0) { long long int cur = m - (bs + mx[i]); nw += cur; bs += cur; rs++; } if (nw < 0) { printf("-1\n"); return 0; } } else { nw += a[i]; if (nw > m) { printf("-1\n"); return 0; } } } printf("%d\n", rs); return 0; }
#include <bits/stdc++.h> using namespace std; void read(bool out = 0) {} long long a[500009]; long long mx[500009]; int main() { read(); long long n, d; cin >> n >> d; for (int i = 0; i < n; i++) cin >> a[i]; for (int i = 1; i < n; i++) a[i] += a[i - 1]; mx[n] = -1e15; bool done = 1; for (int i = n - 1; i >= 0; i--) { mx[i] = max(mx[i + 1], a[i]); if (mx[i] > d) done = 0; } if (!done) return cout << -1, 0; long long dif = 0; long long ans = 0; for (int i = 1; i < n; i++) { if (a[i] == a[i - 1]) { if (dif + a[i] < 0) { dif += max(d - (mx[i] + dif), 0ll); ans++; if (dif + a[i] < 0) done = 0; } } } if (done) cout << ans; else cout << -1; }
#include <bits/stdc++.h> using namespace std; const int N = 100500; int main() { srand(time(NULL)); ios_base::sync_with_stdio(0); int n; long long d; cin >> n >> d; long long cur = 0; int ans = 0; long long mx = 0; int last = 0; for (int i = 1; i <= n; i++) { long long x; cin >> x; if (x == 0) { if (cur < 0) { if (last == 0) ans++, last = 1, mx = 0, cur = 0; else { cur += (d - mx); last = 0; if (cur < 0) ans++, last = 1, cur = 0, mx = 0; else { mx += -(cur - (d - mx)); last = 1; cur = 0; } } } } else { cur += x; mx = max(cur, mx); if (cur > d) cout << "-1", exit(0); } } cout << ans; return 0; }
#include <bits/stdc++.h> using namespace std; using tint = long long; using ld = long double; using vi = vector<int>; using vl = vector<tint>; using vb = vector<bool>; using pi = pair<int, int>; using pl = pair<tint, tint>; const int MOD = 1e9 + 7; const int mod = 998244353; const int MX = 3e5 + 5; const tint INF = 1e18; const int inf = 2e9; const ld PI = acos(ld(-1)); const ld eps = 1e-8; const int dx[4] = {1, -1, 0, 0}; const int dy[4] = {0, 0, 1, -1}; template <class T> void remDup(vector<T>& v) { sort(begin(v), end(v)); v.erase(unique(begin(v), end(v)), end(v)); } template <class T> bool valid(T x, T y, T n, T m) { return (0 <= x && x < n && 0 <= y && y < m); } void NACHO(string name = "cbarn") { ios_base::sync_with_stdio(0); cin.tie(0); } int main() { NACHO(); int n, d; cin >> n >> d; vi a(n); for (int i = 0; i < int(n); i++) cin >> a[i]; vi suff(n); suff[n - 1] = a[n - 1]; for (int i = n - 2; i >= 0; --i) { suff[i] = max(suff[i + 1] + a[i], a[i]); } int cur = 0; int ret = 0; bool ok = 1; for (int i = 0; i < int(n); i++) { if (a[i] == 0) { if (cur < 0) { ++ret; int sumo = d - suff[i] - cur; if (sumo < 0 || cur + sumo < 0) ok = 0; cur += sumo; } } else { cur += a[i]; if (cur > d) ok = 0; } } if (!ok) ret = -1; cout << ret << "\n"; }
#include <bits/stdc++.h> using namespace std; int n, d; int main() { cin >> n >> d; int a = 0, b = 0, ans = 0, x; for (int i = 1; i <= n; i++) { cin >> x; if (x == 0) { if (a < 0) a = 0; if (b < 0) b = d, ans++; } else { a += x, b += x; if (a > d) return cout << -1 << endl, 0; if (b > d) b = d; } } cout << ans << endl; }
#include <bits/stdc++.h> using namespace std; const int N = 100000; int n; long long d, acc, last, neg; int main() { scanf("%d%lld", &n, &d); acc = 0, last = 0, neg = 0; int ans = 0; long long x; for (int i = 0; i < n; i++) { scanf("%lld", &x); if (!x) { if (acc < 0) { acc = d; last += d; neg = 0; ans++; } else { last = min(last, acc); } } else { acc += x; if (x < 0) neg += -x; if (acc > d) { long long y = acc - d; if (y > last) { puts("-1"); return 0; } else { last -= y; acc = d; } } } } printf("%d\n", ans); return 0; }
#include <bits/stdc++.h> using namespace std; signed main() { ios::sync_with_stdio(false); cin.tie(0); cout << fixed << setprecision(20); long long n, d; cin >> n >> d; long long a[n]; for (int i = 0; i < n; i++) cin >> a[i]; long long now = 0; long long ans = 0; long long pot = 0; long long sum = 0; for (int i = 0; i < n; i++) { now += a[i]; pot = min(pot, d - now); if (now > d) { cout << -1 << endl; return 0; } if (a[i] == 0 && now < 0) { if (ans) { if (-now > pot || -now > sum) { ans++; sum = d; } else { sum += now; } pot = d; } else { ans++; sum = d; pot = d; } now = 0; } } cout << ans << endl; }
#include <bits/stdc++.h> using namespace std; int read() { int x = 0, f = 1; char ch = getchar(); while (ch < '0' || ch > '9') { if (ch == '-') f = -1; ch = getchar(); } while (ch >= '0' && ch <= '9') { x = (x << 3) + (x << 1) + (ch ^ 48); ch = getchar(); } return x * f; } int n, d, a[200005], f[200005], ans, pre; int main() { n = read(); d = read(); for (int i = 1; i <= n; i++) a[i] = read(); f[n] = max(0, a[n]); for (int i = n - 1; i >= 1; i--) f[i] = max(f[i + 1] + a[i], a[i]); for (int i = 1; i <= n; i++) { pre += a[i]; if (pre > d) return puts("-1"), 0; if (a[i] == 0) { if (pre < 0) ans++, pre = d - f[i]; if (pre < 0) return puts("-1"), 0; } } printf("%d\n", ans); }
#include <bits/stdc++.h> using namespace std; const int N = 100100; int n, d, a[N], sum[N]; int T[N << 2], tag[N << 2]; void Update(int o) { T[o] = max(T[o << 1], T[o << 1 | 1]); return; } void Build(int o, int l, int r) { tag[o] = 0; if (l == r) { T[o] = sum[l]; return; } int mid = l + r >> 1; Build(o << 1, l, mid); Build(o << 1 | 1, mid + 1, r); Update(o); return; } void Pushdown(int o) { if (tag[o] == 0) return; T[o << 1] += tag[o], tag[o << 1] += tag[o]; T[o << 1 | 1] += tag[o], tag[o << 1 | 1] += tag[o]; tag[o] = 0; return; } void Modify(int o, int l, int r, int L, int R, int x) { if (L > R) return; if (L <= l && r <= R) { tag[o] += x; T[o] += x; return; } Pushdown(o); int mid = l + r >> 1; if (L <= mid) Modify(o << 1, l, mid, L, R, x); if (R > mid) Modify(o << 1 | 1, mid + 1, r, L, R, x); Update(o); return; } int Query(int o, int l, int r, int L, int R) { if (L > R) return 0; if (L <= l && r <= R) return T[o]; Pushdown(o); int mid = l + r >> 1; if (R <= mid) return Query(o << 1, l, mid, L, R); else if (L > mid) return Query(o << 1 | 1, mid + 1, r, L, R); else return max(Query(o << 1, l, mid, L, R), Query(o << 1 | 1, mid + 1, r, L, R)); } int main() { scanf("%d%d", &n, &d); for (int i = 1; i <= n; i++) scanf("%d", &a[i]), sum[i] = sum[i - 1] + a[i]; Build(1, 1, n); int cnt = 0, cur, fix; for (int i = 1; i <= n; i++) { cur = Query(1, 1, n, i, i); if (cur > d) { cnt = -1; break; } if (a[i] == 0) { if (cur < 0) { fix = d - Query(1, 1, n, i, n); if (fix + cur >= 0) cnt++, Modify(1, 1, n, i, n, fix); else { cnt = -1; break; } } } } printf("%d\n", cnt); return 0; }
#include <bits/stdc++.h> using namespace std; inline int get_int() { int res, c, t = 1; while (!isdigit(c = getchar())) if (c == '-') t = -1; for (res = c - '0'; isdigit(c = getchar());) res = res * 10 + c - '0'; return res * t; } constexpr int MAXN = 100000; int A[MAXN + 10], B[MAXN + 10]; int n, d; int main() { n = get_int(); d = get_int(); int bef = 0; for (int i = 1; i <= n; i++) { int c = get_int(); A[i] = A[i - 1] + c; if (c == 0 && bef + A[i] < 0) { bef += -(A[i] + bef); } if (A[i] + bef > d) { cout << -1 << endl; return 0; } } B[n] = d - A[n]; for (int i = n - 1; i >= 1; i--) { B[i] = min(B[i + 1], d - A[i]); } bef = 0; int cnt = 0; for (int i = 1; i <= n; i++) { if (A[i] - A[i - 1] == 0 && bef + A[i] < 0) { if (bef < B[i]) bef = B[i], cnt++; } } cout << cnt << endl; }
#include <bits/stdc++.h> using namespace std; const int N = 100010; const int mod = (int)1e9 + 7; const int MX = 1000010; long long a[N]; int main() { cin.tie(0); cin.sync_with_stdio(0); long long n, d; cin >> n >> d; for (int i = 0; i < n; ++i) { cin >> a[i]; } int cnt = 0; long long mn = 0, mx = 0; for (int i = 0; i < n; ++i) { if (a[i] == 0) { if (mx < 0) { mn = 0; mx = d; cnt++; } mn = max(mn, 0ll); } else { mn += a[i]; mx += a[i]; } mx = min(mx, d); if (mn > d || mx < mn) { puts("-1"); return 0; } } cout << cnt << endl; return 0; }
#include <bits/stdc++.h> using namespace std; int main() { ios ::sync_with_stdio(0); int n, d; cin >> n >> d; int ans = 0; int mx = 0, nw = 0, lst = -1; for (int i = 0; i < n; ++i) { int a; cin >> a; if (a == 0) { if (!~lst || mx > d - max(0, -nw)) { if (nw < 0) ++ans, lst = i; nw += max(0, -nw); mx = 0; } else { mx += max(0, -nw); nw += max(0, -nw); } } else { mx = max(mx, nw += a); } if (mx > d) { cout << -1 << endl; return 0; } } cout << ans << endl; return 0; }
#include <bits/stdc++.h> using namespace std; mt19937 rnd(chrono::high_resolution_clock::now().time_since_epoch().count()); mt19937_64 rnd64( chrono::high_resolution_clock::now().time_since_epoch().count()); template <typename T = int> inline T read() { T res = 0, sig = 1; char c = getchar(); while ((c < '0' || c > '9') && c != '-') { c = getchar(); } if (c == '-') { sig = -1; c = getchar(); } while (c >= '0' && c <= '9') { res = res * 10 + c - '0'; c = getchar(); } return res * sig; } void setio(const char *in, const char *out) { freopen(in, "r", stdin); freopen(out, "w", stdout); } void setio(const string task) { freopen((task + ".in").c_str(), "r", stdin); freopen((task + ".out").c_str(), "w", stdout); } void fastio() { ios::sync_with_stdio(false); cin.tie(0); } int main() { fastio(); int n; long long d; cin >> n >> d; vector<long long> a(n); for (int i = 0; i < n; i++) { cin >> a[i]; } vector<long long> pr(n); pr[0] = a[0]; for (int i = 1; i < n; i++) { pr[i] = pr[i - 1] + a[i]; } for (int i = 0; i < n; i++) { if (pr[i] > d) { cout << "-1\n"; return 0; } } vector<long long> su(n); su[n - 1] = pr[n - 1]; for (int i = n - 2; i > -1; i--) { su[i] = max(su[i + 1], pr[i]); } int ans = 0; long long cur = 0; for (int i = 0; i < n; i++) { if (a[i] == 0 && pr[i] + cur < 0) { ans++; long long mx = d - (su[i] + cur); if (mx < -(pr[i] + cur)) { cout << "-1\n"; return 0; } cur += mx; } } cout << ans; return 0; }
#include <bits/stdc++.h> using namespace std; const int N = 2e5 + 5, inf = 0x3f3f3f3f; long long n, d, a[N]; long long f[N], w[N], mk[N]; int main() { cin >> n >> d; for (int i = 1; i <= n; i++) scanf("%I64d", &a[i]); bool flag = true; long long sum = 0; for (int i = 1; i <= n; i++) { sum += a[i]; if (sum > d) flag = false; if (a[i] == 0 && sum < 0) sum = 0, mk[i] = -1; w[i] = sum; } if (flag == false) puts("-1"); else { sum = 0; for (int i = n; i >= 1; i--) { if (mk[i + 1] == -1) f[i] = w[i]; else f[i] = max(f[i + 1], w[i]); } long long ans = 0, val = 0; for (int i = 1; i <= n; i++) { sum += a[i]; if (a[i] == 0 && sum < 0) { ans++; long long exd = max(0ll, d - f[i]); sum = exd; } } printf("%I64d\n", ans); } return 0; }
#include <bits/stdc++.h> using namespace std; const long long mod = 1e9 + 7; int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); ; int n, d; cin >> n >> d; int a[n], b[n], mx[n]; int temp = 0; for (int i = 0; i < n; ++i) { cin >> a[i]; temp += a[i]; b[i] = temp; } mx[n - 1] = b[n - 1]; for (int i = n - 2; i >= 0; --i) { mx[i] = max(mx[i + 1], b[i]); } int ans = 0; temp = 0; for (int i = 0; i < n; ++i) { if (b[i] + temp > d) { cout << -1 << '\n'; ; return 0; } if (a[i] == 0) { if (b[i] + temp < 0) { int curr = 0 - b[i] - temp; if (curr > d - (mx[i] + temp)) { cout << -1 << '\n'; ; return 0; } curr = d - mx[i] - temp; temp += curr; ans++; } } } cout << ans << '\n'; ; return 0; }
#include <bits/stdc++.h> char ch; bool fs; void re(long long& x) { while (ch = getchar(), ch < 33) ; if (ch == '-') fs = 1, x = 0; else fs = 0, x = ch - 48; while (ch = getchar(), ch > 33) x = x * 10 + ch - 48; if (fs) x = -x; } using namespace std; long long n, d, ans, a[100005], mx[100005]; int main() { re(n), re(d); for (long long i = 1; i <= n; ++i) re(a[i]); for (long long i = n; i; --i) mx[i] = max(0ll, max(a[i], a[i] + mx[i + 1])); for (long long i = 1, now = 0; i <= n; ++i) { now += a[i]; if (now > d || (!a[i] && mx[i] > d)) { puts("-1"); return 0; } } for (long long i = 1, now = 0; i <= n; ++i) { now += a[i]; if (!a[i] && now < 0) { ++ans; now = d - mx[i]; } } printf("%I64d\n", ans); }
#include <bits/stdc++.h> int n, d; const int N = 1e5 + 1; int val[N]; int mx[N]; int main() { scanf("%d%d", &n, &d); for (int i = 0; i < n; i++) scanf("%d", val + i); mx[n - 1] = val[n - 1]; for (int i = n - 2; i >= 0; i--) mx[i] = std::max(val[i], val[i] + mx[i + 1]); int balance = 0; int ans = 0; for (int i = 0; i < n; i++) { if (val[i] != 0) { balance += val[i]; if (balance > d) { printf("-1\n"); return 0; } } else { if (balance < 0) { ans++; int m = std::min(d, d - mx[i]) - balance; if (m > 0) balance += m; if (balance < 0) { printf("-1\n"); return 0; } } } } printf("%d\n", ans); }
#include <bits/stdc++.h> using namespace std; int main() { int n, d; scanf("%d%d", &n, &d); int mn = 0, mx = 0; int ans = 0; for (int i = 1, x; i <= n; ++i) { scanf("%d", &x); if (x) { mn += x, mx += x; if (mn > d) return puts("-1"), 0; mx = min(mx, d); } else { if (mx >= 0) mn = max(mn, 0); else ++ans, mn = 0, mx = d; } } printf("%d\n", ans); }
#include <bits/stdc++.h> using namespace std; const int N = 1e5 + 5; int n, d; int a[N]; int pref[N]; void solve() { scanf("%d %d", &n, &d); for (int i = 1; i <= n; ++i) { scanf("%d", a + i); } for (int i = 1; i <= n; ++i) { pref[i] += pref[i - 1] + a[i]; } long long u = 0, v = 0; int ans = 0; for (int i = 1; i <= n; ++i) { u += a[i], v += a[i]; if (u > d) { printf("-1\n"); return; } v = min(v, 1LL * d); if (!a[i] and u < 0) { if (v < 0) { ans++; u = 0, v = d; } else { u = 0; } } } printf("%d\n", ans); } int main() { solve(); return 0; }
#include <bits/stdc++.h> using namespace std; const long long MAX = 1e5 + 47; long long a[MAX], sum[MAX], n, ans, tek, d, last_iter[MAX], li, t; void go() { cin >> n >> d; for (long long i = 1; i <= n; i++) cin >> a[i], sum[i] = sum[i - 1] + a[i]; for (long long i = n - 1; i >= 1; i--) sum[i] = max(sum[i], sum[i + 1]); return; } int main() { go(); for (long long i = 1; i <= n; i++) { long long v = a[i]; tek += v; if (tek > d) { cout << -1; return 0; } if (!v) { if (tek < 0) { sum[i] += li; t = max(0ll, min(d - tek, d - sum[i])); ans++; li += t; tek += t; if (tek < 0) { cout << -1; return 0; } } } } cout << ans; }
#include <bits/stdc++.h> using namespace std; int v[100005]; int s[100005]; int maxx[100005]; int main() { int n, d, i, sold, creditare, ok = 0; scanf("%d%d", &n, &d); for (i = 1; i <= n; i++) { scanf("%d", &v[i]); s[i] = s[i - 1] + v[i]; maxx[i] = -1000000000; } maxx[n + 1] = -1000000000; for (i = n; i > 0; i--) maxx[i] = max(maxx[i + 1], s[i]); sold = 0; creditare = 0; for (i = 1; i <= n; i++) { if (v[i] == 0) { if (sold < 0) { creditare++; if (d < maxx[i] - s[i - 1]) ok = 1; else sold = d - (maxx[i] - s[i - 1]); } } else sold += v[i]; if (sold > d) ok = 1; } if (ok == 1) printf("-1"); else printf("%d", creditare); return 0; }
#include <bits/stdc++.h> int n, a[100005], cnt, ans; long long d, l[100005], r[100005], sum[100005]; int main() { scanf("%d %lld", &n, &d); for (int i = 1; i <= n; i++) { scanf("%d", a + i); sum[i] = sum[i - 1] + a[i]; } long long mx = -1e15; for (int i = n; i >= 1; i--) { mx = std::max(sum[i], mx); if (mx > d) return !printf("-1"); r[i] = d - mx; } for (int i = 1; i <= n; i++) { if (a[i] == 0) { if (sum[i] + r[i] < 0) return !printf("-1"); if (sum[i] + cnt < 0) { ++ans; cnt = r[i]; } } } printf("%d", ans); }
#include <bits/stdc++.h> using namespace std; const int maxn = 1e5 + 1; int n, d; int a[maxn]; int lim[maxn]; int main() { scanf("%d %d", &n, &d); for (int i = 1; i <= n; ++i) scanf("%d", a + i); lim[n] = d; for (int i = n - 1; i >= 0; --i) { lim[i] = min(d, lim[i + 1] - a[i + 1]); if (a[i] == 0 && lim[i] < 0) { puts("-1"); return 0; } } int ans = 0, cur = 0; for (int i = 1; i <= n; ++i) { cur += a[i]; if (cur > d) { puts("-1"); return 0; } if (a[i] == 0 && cur < 0) { ++ans; cur = lim[i]; } } printf("%d\n", ans); }
#include <bits/stdc++.h> using namespace std; const int N = 1e5 + 7; const int INF = 0x3f3f3f3f; int n, d; int a[N], s[N]; int z[N], cnt = 0; int cm[N]; int main() { scanf("%d%d", &n, &d); for (int i = 1; i <= n; ++i) { scanf("%d", &a[i]); s[i] = s[i - 1] + a[i]; if (a[i] == 0) z[cnt++] = i; } cm[n + 1] = INF; for (int i = n; i >= 1; --i) { cm[i] = min(d - s[i], cm[i + 1]); if (cm[i] < 0) { printf("-1\n"); return 0; } } int answer = 0; int ans = 0; for (int i = 0; i < cnt; ++i) { s[z[i]] += ans; if (s[z[i]] >= 0) continue; s[z[i]] += (cm[z[i]] - ans); if (s[z[i]] < 0) { printf("-1\n"); return 0; } ans = cm[z[i]]; answer++; } printf("%d\n", answer); return 0; }
#include <bits/stdc++.h> using namespace std; long long int arr[100005], fx[100005]; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n; long long int d; cin >> n >> d; long long int cart = 0; long long int a = 0; arr[0] = 0; for (int i = 1; i <= n; i++) { int c; cin >> c; arr[i] = arr[i - 1] + c; if (c == 0) { if (arr[i] < 0) { a += -arr[i]; arr[i] = 0; } } fx[i] = a; if (arr[i] > d) { cout << "-1"; return 0; } } for (int i = n - 1; i > 0; i--) { if (fx[i] != fx[i + 1]) { if (arr[i] + (fx[i + 1] - fx[i]) <= d) { arr[i] += fx[i + 1] - fx[i]; fx[i] = fx[i + 1]; } } } long long int check = 0; long long int cc = 0; for (int i = 1; i <= n; i++) { if (check != fx[i]) { check = fx[i]; cc++; } } cout << cc; return 0; }
#include <bits/stdc++.h> using namespace std; const long long INF = 2e9 + 13; const long long MOD = 1e9 + 7; int N, D; int V[300005]; int suf[300005]; int main(void) { ios_base::sync_with_stdio(false); cin.tie(0); cin >> N >> D; for (int i = 1; i <= N; i++) { cin >> V[i]; } for (int i = N; i >= 1; i--) suf[i] = max(suf[i + 1] + V[i], 0); int csum = 0; int ans = 0; for (int i = 1; i <= N; i++) { if (V[i] == 0 && csum < 0) { ans++; csum = D - suf[i]; if (csum < 0) { cout << -1 << endl; return 0; } } csum += V[i]; if (csum > D) { cout << -1 << endl; return 0; } } cout << ans << endl; }
#include <bits/stdc++.h> using namespace std; const int N = 1e5 + 5; int a[N], maxInc[N]; int n, d; int main() { scanf("%d%d", &n, &d); for (int i = 1; i <= n; i++) { scanf("%d", &a[i]); } maxInc[n + 1] = 0; for (int i = n; i >= 1; --i) maxInc[i] = max(0, a[i] + max(0, maxInc[i + 1])); long long cur = 0; int res = 0; for (int i = 1; i <= n; ++i) if (a[i] != 0) { cur += a[i]; if (cur > d) { printf("-1"); exit(0); } } else { if (cur < 0) { res++; cur = d - maxInc[i + 1]; if (cur < 0) { printf("-1"); exit(0); } } } printf("%d", res); return 0; }
#include <bits/stdc++.h> using namespace std; long long gcd1(long long a, long long b) { if (a == 0) return b; return gcd1(b % a, a); } long long modx(long long base, long long ex) { long long ans = 1LL, val = base; while (ex > 0LL) { if (ex & 1LL) ans = (ans * val) % 1000000007LL; val = (val * val) % 1000000007LL; ex = ex >> 1LL; } return ans; } const int maxn = 1e5 + 10; long long n, d; long long a[maxn], maxx[maxn], pre[maxn]; bool flag; vector<int> v; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> n >> d; for (int i = 1; i <= n; i++) { cin >> a[i]; pre[i] = pre[i - 1] + a[i]; if (pre[i] > d) flag = true; if (a[i] == 0) v.push_back(i); } if (flag) { cout << -1 << endl; return 0; } maxx[n + 1] = INT_MIN; for (int i = n; i >= 1; i--) maxx[i] = max(pre[i], maxx[i + 1]); long long temp = 0, cnt = 0; for (int i = 0; i < v.size(); i++) { if (pre[v[i]] + temp < 0) { long long t = maxx[v[i]] + temp; cnt++; long long add = d - t; if (add + pre[v[i]] + temp < 0) { cout << -1 << endl; return 0; } temp += add; } } cout << cnt << endl; return 0; }
#include <bits/stdc++.h> using namespace std; const int maxn = 100010; int bourles[maxn]; pair<int, int> checked[maxn]; void compress(int n, int& m) { int p = 1; m = 0; while (p <= n) { int cnt = 0; while (p <= n && (bourles[p] || (!bourles[p] && cnt >= 0))) cnt += bourles[p++]; if (p <= n) checked[++m] = pair<int, int>(p, cnt); } } void solve(int& ret, int n, int m, int d) { ret = 0; int p = 1; while (p <= m) { ++ret; int L = p, R = m; while (L < R) { int mid = L + ((R - L + 1) >> 1); int cnt = 0; for (int i = p + 1; i <= mid; ++i) cnt += checked[i].second; bool ok = true; int somemax = -cnt; for (int i = checked[p].first; i <= checked[mid].first; ++i) { somemax += bourles[i]; if (somemax > d) { ok = false; break; } } if (ok) L = mid; else R = mid - 1; } p = L + 1; } } int main() { int n, d; while (~scanf("%d%d", &n, &d)) { int somemax = 0, zero = 0; bool ok = true; for (int i = 1; i <= n; ++i) { scanf("%d", bourles + i); if (!bourles[i]) { ++zero; if (somemax < 0) somemax = 0; } if (bourles[i] + somemax > d) ok = false; somemax += bourles[i]; } if (!ok) { puts("-1"); continue; } int m; compress(n, m); if (!zero || !n) { puts("0"); continue; } int ret; solve(ret, n, m, d); printf("%d\n", ret); } return 0; }
#include <bits/stdc++.h> using namespace std; const int maxN = 1e5 + 11; typedef int i_N[maxN]; int N, D; i_N A, B, C; int ans; int main() { cin >> N >> D; for (int i = 1; i <= N; i++) cin >> A[i], B[i] = B[i - 1] + A[i]; C[N] = B[N]; for (int i = N - 1; i >= 1; i--) C[i] = max(C[i + 1], B[i]); int S = (1 - 1); for (int i = 1; i <= N; i++) { if (B[i] + S > D) { cout << -1; return (1 - 1); } if (A[i] == (1 - 1)) { if (B[i] + S >= (1 - 1)) continue; int t = max((1 - 1), D - C[i] - S); if (B[i] + t + S < (1 - 1)) { cout << -1; return (1 - 1); } S += t; ans++; } } cout << ans; }
#include <bits/stdc++.h> using namespace std; const int MAX_N = 1e5 + 5; int n, d; int a[MAX_N]; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cin >> n >> d; for (int i = 0; i < n; i++) cin >> a[i]; int ans = 0; long long s = 0, e = 0; for (int i = 0; i < n; i++) { s += a[i]; if (s > d) { ans = -1; break; } if (s + e > d) e = d - s; if (a[i] == 0 && s < 0) { if (s + e < 0) { s = 0; e = d; ans++; } else { e = s + e; s = 0; } } } cout << ans << "\n"; return 0; }
#include <bits/stdc++.h> using namespace std; int main() { int n; long long int d; cin >> n >> d; vector<int> check_days; int a[n]; long long int prefix_sum[n]; cin >> a[0]; if (a[0] == 0) check_days.push_back(0); prefix_sum[0] = a[0]; for (int i = 1; i < n; i++) { cin >> a[i]; if (a[i] == 0) check_days.push_back(i); prefix_sum[i] = prefix_sum[i - 1] + a[i]; } int days = 0; long long int mi = LLONG_MIN; long long int max_balance_after[n]; for (int i = n - 1; i >= 0; i--) { if (prefix_sum[i] > d) { cout << -1; return 0; } mi = max(mi, prefix_sum[i]); max_balance_after[i] = mi; } int deposit_val = 0; for (int i = 0; i < check_days.size(); i++) { int j = check_days[i]; if (prefix_sum[j] + deposit_val < 0) { int new_bal = 0, val_to_deposit = 0; new_bal = max_balance_after[j] + deposit_val; val_to_deposit = d - new_bal; days++; deposit_val += val_to_deposit; if (prefix_sum[j] + deposit_val < 0) { cout << -1; return 0; } } } cout << days; return 0; }
#include <bits/stdc++.h> using namespace std; long long n; long long k, m; long long mod = 1e9 + 7; vector<int> ar, tag; vector<vector<int> > graph; int main() { std::ios_base::sync_with_stdio(false); std::cin.tie(nullptr); long long d; cin >> n >> d; vector<long long> acc; for (int a, i = 0; i < n; i++) { cin >> a; ar.push_back(a); if (i) acc.push_back(acc[i - 1] + a); else acc.push_back(a); if (acc[i] > d) return cout << -1, 0; } vector<long long> postmax(n + 1, 0); for (int i = n - 1; i >= 0; i--) { if (i == n - 1) postmax[i] = (acc[i]); else postmax[i] = (max(acc[i], postmax[i + 1])); } postmax.push_back(INT_MIN); long long addon = 0; int count = 0; for (int i = 0; i < n; i++) { if (ar[i] == 0 && acc[i] + addon < 0 && i != n - 1) { long long minval = 0 - (acc[i] + addon); long long maxval = d - (postmax[i + 1] + addon); if (minval > maxval) return cout << -1, 0; addon += min(maxval, d + minval); count++; } } if (acc[n - 1] + addon < 0 && ar[n - 1] == 0) count++; return cout << count, 0; }
#include <bits/stdc++.h> using namespace std; int main() { int n_days, limit, balance = 0, min_deposits = 0, trans[100000], exceed_limit = 0; int max_deposits[100000], temp_max, n_deposits = 0, deposit_index, running_dep = 0; cin >> n_days >> limit; for (int j = 0; j < n_days; j++) { cin >> trans[j]; balance += trans[j]; if (!trans[j]) { n_deposits++; } } deposit_index = n_deposits; temp_max = limit - balance; for (int j = n_days - 1; j >= 0; j--) { if (!trans[j]) { max_deposits[deposit_index - 1] = max(-balance, temp_max); deposit_index--; } balance -= trans[j]; temp_max = min(temp_max, limit - balance); } running_dep = 0; for (int j = 0; j < n_days; j++) { balance += trans[j]; if (!trans[j]) { if (balance < 0) { min_deposits++; balance += max(0, max_deposits[deposit_index] - running_dep); running_dep = max(running_dep, max_deposits[deposit_index]); } deposit_index++; } if (balance > limit) { exceed_limit = 1; break; } } if (exceed_limit) { cout << -1 << endl; } else { cout << min_deposits << endl; } return 0; }
#include <bits/stdc++.h> using namespace std; const double pi = 3.141592653589; const long long int zero = 0; long long int mod = 1000000007; const long long int inf = 1e9; const long long int inff = 1000000000000000000; char digits[10] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9'}; char al[26] = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'}; char capital[26] = {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'}; int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); int t = 1; while (t--) { long long int n, d; cin >> n >> d; long long int arr[n]; for (int i = 0; i < n; i += 1) { cin >> arr[i]; } long long int ma[n]; ma[n - 1] = d; for (int i = n - 2; i >= 0; i -= 1) { ma[i] = ma[i + 1] - arr[i + 1]; ma[i] = min(ma[i], d); } long long int sum = 0, ans = 0; for (int i = 0; i < n; i += 1) { if (arr[i] == 0) { if (sum < 0) { ans++; sum = ma[i]; if (sum < 0) { sum = 0; } if (sum > ma[i]) { cout << "-1\n"; return 0; } } } else { sum += arr[i]; if (sum > ma[i]) { cout << "-1\n"; return 0; } } } cout << ans; } }
#include <bits/stdc++.h> using namespace std; int a[100001], b[100001], c[100001]; int main() { int n, d, ans = 0; scanf("%d%d", &n, &d); for (int i = 1; i <= n; ++i) scanf("%d", &a[i]); for (int i = 1; i <= n; ++i) { b[i] = a[i] + b[i - 1]; c[i] = max(0, d - b[i]); } for (int i = n - 1; i > 0; --i) c[i] = min(c[i], c[i + 1]); int excess = 0; for (int i = 1; i <= n; ++i) { if ((a[i] == 0 && c[i] + b[i] < 0) || c[i] + b[i] > d) { ans = -1; break; } if (a[i] != 0) continue; if (excess + b[i] < 0) { ++ans; excess = c[i]; } } printf("%d\n", ans); return 0; }
#include <bits/stdc++.h> using namespace std; const int MAXN = 1e5 + 7; int main() { int n, d; scanf("%d%d", &n, &d); int a, MAX = 0, MIN = 0, ans = 0; bool flag = false; for (int i = 1; i <= n; i++) { scanf("%d", &a); if (flag) continue; if (a == 0) { if (MIN < 0) MIN = 0; if (MAX < 0) { MAX = d; ans++; } } else { MIN += a; MAX += a; if (MIN > d) { printf("-1\n"); flag = true; } if (MAX > d) { MAX = d; } } } if (!flag) printf("%d\n", ans); }
#include <bits/stdc++.h> using namespace std; void solve() { long long n, d; cin >> n >> d; vector<long long> a(n); for (long long i = 0; i < n; i++) cin >> a[i]; long long sum = 0, mx = INT_MIN, ans = 0; bool flag = true, aagaya = false; for (long long i = 0; i < n; i++) { if (a[i] == 0) { if (sum < 0) { if (aagaya && (d - mx) >= 0 && (d - mx) + sum >= 0) { mx += abs(sum); sum = 0; } else { ans++; mx = 0; sum = 0; } aagaya = true; } } else { sum += a[i]; mx = max(mx, sum); } if (mx > d) flag = false; } if (flag) cout << ans << endl; else cout << -1 << endl; } signed main() { ios_base::sync_with_stdio(false); cin.tie(NULL); long long t = 1; while (t--) solve(); return 0; }
#include <bits/stdc++.h> using namespace std; long long arr[100001], sum[100001], mx[100001]; int main() { ios_base::sync_with_stdio(0); cin.tie(0); int n, d; cin >> n >> d; for (auto i = 0; i < (long long)(n); i++) { cin >> arr[i]; sum[i] = arr[i]; if (i) sum[i] += sum[i - 1]; } mx[n - 1] = sum[n - 1]; for (int i = n - 2; i >= 0; i--) mx[i] = max(mx[i + 1], sum[i]); long long curr = 0, ret = 0; for (auto i = 0; i < (long long)(n); i++) { if (sum[i] + curr > d) return cout << -1, 0; if (!arr[i] && sum[i] + curr < 0) { long long extra = d - (mx[i] + curr); if (sum[i] + curr + extra < 0) return cout << -1, 0; ret++; curr += extra; } } cout << ret << '\n'; return 0; }
#include <bits/stdc++.h> using namespace std; const int MAXN = 100005; long long n, d, sol, ofs, curr; int l[MAXN]; long long sum[MAXN], maxi[MAXN]; int main() { cin >> n >> d; for (int i = 0; i < n; i++) { scanf("%d", &l[i]); if (i == 0) sum[i] = l[i]; else sum[i] = sum[i - 1] + l[i]; } maxi[n] = -1000000007; for (int i = n - 1; i >= 0; i--) { maxi[i] = max(maxi[i + 1], sum[i]); } for (int i = 0; i < n; i++) { long long naj = maxi[i] + ofs; if (naj > d) { cout << -1; return 0; } if (l[i] != 0) { curr += l[i]; } else if (curr < 0) { sol++; ofs += d - naj; curr += d - naj; if (curr < 0) { cout << -1; return 0; } } } cout << sol; return 0; }
#include <bits/stdc++.h> using namespace std; const int N = 1e5 + 2, oo = 1e9; int n, a[N], ma[N], b[N]; long long lim; void err() { cout << -1; exit(0); } int main() { ios::sync_with_stdio(false); cin.tie(NULL); cin >> n >> lim; for (int i = 1; i <= n; i++) cin >> a[i], b[i] = b[i - 1] + a[i]; ma[n] = -oo; for (int i = n - 1; i > 0; i--) ma[i] = max(ma[i + 1], b[i + 1]); long long acc = 0; long long s = 0; int cnt = 0; for (int i = 1; i <= n; i++) if (a[i] != 0) { acc += a[i]; if (acc > lim) err(); } else if (acc < 0) { cnt++; long long delta = min(lim - (ma[i] + s), lim - acc); acc += delta; s += delta; if (acc < 0) err(); } cout << cnt; }
#include <bits/stdc++.h> using namespace std; const int N = 1e5 + 5; int n, d; int a[N]; int main() { scanf("%d%d", &n, &d); long long sm = 0; for (int i = 0; i < n; ++i) { scanf("%d", &a[i]); sm += a[i]; if (a[i] == 0 && sm < 0) sm = 0; if (sm > d) { cout << -1; return 0; } } long long sum = 0; int cnt = 0, check = 0; for (int i = 0; i < n; ++i) { sum += a[i]; if (sum > d) { check -= (sum - d); sum = d; if (check < 0) { cout << -1; return 0; } } if (a[i] == 0 && sum < 0) { cnt++; sum = check = d; } } cout << cnt; }
#include <bits/stdc++.h> using namespace std; signed main() { ios_base::sync_with_stdio(false); cin.tie(NULL); long long n, m; cin >> n >> m; long long a[n + 1]; long long pre[n + 1]; pre[0] = 0; for (long long i = 1; i <= n; i++) { cin >> a[i]; pre[i] = pre[i - 1] + a[i]; } long long max_in_pre_i_to_n[n + 1]; max_in_pre_i_to_n[n] = pre[n]; for (long long i = n - 1; i >= 1; i--) { max_in_pre_i_to_n[i] = max(pre[i], max_in_pre_i_to_n[i + 1]); } long long sum_till_here = 0; long long extra_deposit = 0; long long times = 0; for (long long i = 1; i <= n; i++) { sum_till_here += a[i]; if (sum_till_here > m) { cout << "-1\n"; return 0; } if (a[i] == 0 && sum_till_here < 0) { long long how_much_i_have_to_deposit = -1 * sum_till_here; long long next_max_value = max_in_pre_i_to_n[i] - pre[i]; if (next_max_value > m) { cout << "-1\n"; return 0; } sum_till_here = m - next_max_value; times++; } } cout << times << "\n"; }
#include <bits/stdc++.h> using namespace std; long long mod = 1e9 + 7; long long modexp(long long x, long long n) { if (n == 0) return 1; if (n % 2 == 0) { long long y = modexp(x, n / 2) % mod; return (y * y) % mod; } return (x * modexp(x, n - 1) % mod) % mod; } int main() { int n; long long d; scanf("%d%lld", &n, &d); long long a[n]; for (int i = 0; i < n; i++) { scanf("%lld", &a[i]); } long long curr = 0; long long sum = 0; for (int i = 0; i < n; i++) { sum += a[i]; if (a[i] == 0) { if (sum < 0) sum = 0; } if (sum > d) { printf("-1\n"); return 0; } } long long x = 0; int ans = 0; for (int i = 0; i < n; i++) { if (a[i] > 0) { curr += a[i]; } else if (a[i] < 0) { curr += a[i]; } else { if (curr < 0) { if (abs(curr) <= x) { x = x - abs(curr); } else { ans++; x = d; } curr = 0; } } if (curr > d) { printf("-1\n"); return 0; } x = min(x, d - curr); } cout << ans << endl; return 0; }
#include <bits/stdc++.h> using namespace std; long long modpow(long long a, long long n, long long temp) { long long res = 1; while (n > 0) { if (n & 1) res = (res * a) % temp; a = (a * a) % temp; n >>= 1; } return res % temp; } int main() { ios_base::sync_with_stdio(false); cout.precision(10); cout << fixed; int n, d; cin >> n >> d; int i; vector<int> a(n), max1(n), c(n); for (i = 0; i < n; i++) cin >> a[i]; c[0] = a[0]; for (i = 1; i < n; i++) c[i] = c[i - 1] + a[i]; max1[n - 1] = c[n - 1]; for (i = n - 2; i >= 0; i--) max1[i] = max(c[i], max1[i + 1]); int add = 0; int cnt = 0; bool pos = 1; for (i = 0; i < n; i++) { if (c[i] + add > d) pos = 0; if (a[i] == 0 && c[i] + add < 0) { int canAdd = d - (max1[i] + add); if (c[i] + add + canAdd < 0 || canAdd < 0) pos = 0; else { add += canAdd; cnt++; } } } if (pos) cout << cnt << "\n"; else cout << "-1\n"; if (0) cerr << "Time elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " s.\n"; return 0; }
#include <bits/stdc++.h> using namespace std; int main() { int n, d, x; cin >> n >> d; int tk = 0, mx = 0; int ans = 0; while (n--) { cin >> x; if (x != 0) { tk += x, mx += x; if (tk > d) { cout << "-1"; exit(0); } mx = min(mx, d); } else { if (mx >= 0) tk = max(tk, 0); else ans++, tk = 0, mx = d; } } cout << ans; }
#include <bits/stdc++.h> using namespace std; int main() { int n, d, a[100009]; scanf("%d%d", &n, &d); for (int i = 0; i < n; i++) { cin >> a[i]; } int ans = 0; int Min = 0, Max = 0; for (int i = 0; i < n; i++) { if (!a[i]) { if (Min < 0) Min = 0; if (Max < 0) { ans++; Max = d; } } else { Min += a[i]; Max += a[i]; if (Max > d) Max = d; if (Min > d) { printf("-1\n"); return 0; } } } printf("%d\n", ans); return 0; }
#include <bits/stdc++.h> inline long long read() { char c = getchar(); while (c != '-' && (c < '0' || c > '9')) c = getchar(); long long k = 0, kk = 1; if (c == '-') c = getchar(), kk = -1; while (c >= '0' && c <= '9') k = k * 10 + c - '0', c = getchar(); return kk * k; } using namespace std; void write(long long x) { if (x < 0) x = -x, putchar('-'); if (x / 10) write(x / 10); putchar(x % 10 + '0'); } void writeln(long long x) { write(x); puts(""); } long long n, d, a[1000010], l, r, ans; signed main() { n = read(); d = read(); l = r = 0; for (long long i = 1; i <= n; i++) { a[i] = read(); l += a[i]; r += a[i]; if (l > d) { puts("-1"); return 0; } r = min(r, d); if (!a[i] && l < 0) { l = max(l, 0ll); if (r < 0) ans++, r = d; } } writeln(ans); }
#include <bits/stdc++.h> using namespace std; int n, d, x, ans; int main() { scanf("%d%d", &n, &d); int vup = 0, vdn = 0; while (n--) { scanf("%d", &x); if (x) { vup += x; if (vup > d) vup = d; vdn += x; } else { vdn = max(vdn, 0); if (vdn > vup) ++ans, vup = d, vdn = 0; } if (vdn > vup) return puts("-1"), 0; } printf("%d", ans); return 0; }
#include <bits/stdc++.h> using namespace std; using ll = long long; using pii = pair<int, int>; using vi = vector<int>; const int MN = 1e5 + 5; int a[MN], ed[MN]; void die() { cout << -1 << '\n'; exit(0); } int main() { ios_base::sync_with_stdio(0), cin.tie(0); int n, d; cin >> n >> d; for (int i = 1; i <= n; i++) cin >> a[i]; int cur = 0, mx = 0, ans = 0; for (int i = 1; i <= n; i++) { if (a[i] == 0) { if (cur + mx < 0) { ans++, cur = 0, mx = d; } else if (cur < 0) { mx += cur; cur = 0; } } else { cur += a[i]; if (cur > d) die(); mx = min(mx, d - cur); } } cout << ans << '\n'; return 0; }
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int a = 0, b = 0; int n, d, t; cin >> n >> d; int sol = 0; for (int i = 0; i < n; ++i) { cin >> t; if (t == 0) { if (b + t < 0) { a = 0; b = d; sol++; } else { a = max(0, a); } } else if (a + t > d) { sol = -1; break; } a = min(d, a + t); b = min(d, b + t); } cout << sol; return 0; }
#include <bits/stdc++.h> using namespace std; struct A { int l, r; int nxl, nxr; int big; int add; } Node[200005]; int all[200005]; int add[200005] = {0}; int now = 1; void build(int l, int r, int here) { Node[here].l = l; Node[here].r = r; Node[here].add = 0; if (l == r) { Node[here].big = add[l]; return; } Node[here].nxl = now++; Node[here].nxr = now++; build(l, (l + r) / 2, Node[here].nxl); build((l + r) / 2 + 1, r, Node[here].nxr); Node[here].big = max(Node[Node[here].nxl].big, Node[Node[here].nxr].big); } void UPD(int here) { Node[Node[here].nxl].big += Node[here].add; Node[Node[here].nxr].big += Node[here].add; Node[Node[here].nxl].add += Node[here].add; Node[Node[here].nxr].add += Node[here].add; Node[here].add = 0; } int Find(int l, int r, int here) { if (l == Node[here].l && r == Node[here].r) return Node[here].big; UPD(here); if (r <= (Node[here].l + Node[here].r) / 2) return Find(l, r, Node[here].nxl); else if (l > (Node[here].l + Node[here].r) / 2) return Find(l, r, Node[here].nxr); else return max(Find(l, (Node[here].l + Node[here].r) / 2, Node[here].nxl), Find((Node[here].l + Node[here].r) / 2 + 1, r, Node[here].nxr)); } void Add(int l, int r, int here, int con) { if (l == Node[here].l && r == Node[here].r) { Node[here].add += con; Node[here].big += con; return; } UPD(here); if (r <= (Node[here].l + Node[here].r) / 2) Add(l, r, Node[here].nxl, con); else if (l > (Node[here].l + Node[here].r) / 2) Add(l, r, Node[here].nxr, con); else { Add(l, (Node[here].l + Node[here].r) / 2, Node[here].nxl, con); Add((Node[here].l + Node[here].r) / 2 + 1, r, Node[here].nxr, con); } Node[here].big = max(Node[Node[here].nxl].big, Node[Node[here].nxr].big); } int main() { int N, D, ok = 1, last = -1, i, ans = 0; scanf("%d %d", &N, &D); for (i = 1; i <= N; i++) { scanf("%d", &all[i]); add[i] = add[i - 1] + all[i]; } build(0, N, 0); for (i = 1; i <= N; i++) { if (all[i] == 0) { if (Find(i, i, 0) >= 0) continue; if (last == -1 || Find(last, N, 0) + (0 - Find(i, i, 0)) > D) { if (Find(i, N, 0) + (0 - Find(i, i, 0)) > D) ok = 0; else { Add(i, N, 0, (0 - Find(i, i, 0))); last = i; ans++; } } else Add(last, N, 0, (0 - Find(i, i, 0))); } } if (Find(0, N, 0) > D) ok = 0; if (!ok) printf("-1\n"); else printf("%d\n", ans); return 0; }
#include <bits/stdc++.h> using namespace std; const int MaxN = 100003; int n, d; int a[MaxN]; long long pre[MaxN], f[MaxN]; int ans; long long s; int main() { scanf("%d%d", &n, &d); for (int i = 1; i <= n; ++i) scanf("%d", a + i); for (int i = 1; i <= n; ++i) { pre[i] = pre[i - 1] + a[i]; if (pre[i] > d) return puts("-1"), 0; } f[n] = pre[n]; for (int i = n - 1; i >= 1; --i) f[i] = max(pre[i], f[i + 1]); for (int i = 1; i <= n; ++i) f[i] = d - f[i]; ans = 0, s = 0; for (int i = 1; i <= n; ++i) if (a[i] == 0 && pre[i] + s < 0) { ans++; s = f[i]; if (pre[i] + s < 0) return puts("-1"), 0; } printf("%d\n", ans); return 0; }
#include <bits/stdc++.h> using namespace std; void fail() { cout << -1 << endl; exit(0); } int main() { int N, D; cin >> N >> D; vector<int64_t> A(N), S(N + 1); for (int i = 0; i < N; i++) { cin >> A[i]; S[i + 1] = S[i] + A[i]; } for (int i = N - 1; i >= 0; i--) S[i] = max(S[i], S[i + 1]); if (S[0] > D) fail(); int64_t now = 0, ans = 0, gained = 0; for (int i = 0; i < N; i++) { int64_t a = A[i]; if (a == 0 && now < 0) { int64_t avail = D - S[i] - gained; now += avail; if (now < 0) fail(); ans++; gained += avail; } now += a; if (now > D) fail(); } cout << ans << endl; return 0; }
#include <bits/stdc++.h> using namespace std; long long arr[800001]; long long suf[800001]; int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); long long t; t = 1; while (t--) { long long n, d; cin >> n >> d; vector<long long> v1; for (long long i = 0; i < n; i++) { long long num; cin >> num; v1.push_back(num); if (i > 0) { arr[i] += arr[i - 1]; } arr[i] += num; } for (long long i = n - 1; i >= 0; i--) { if (i == n - 1) { suf[i] = arr[i]; } else { suf[i] = max(suf[i + 1], arr[i]); } } long long used = 0; long long ans = 0; for (long long i = 0; i < n; i++) { if (v1[i] != 0) { if (arr[i] + used > d) { cout << "-1"; return 0; } continue; } if (v1[i] == 0 && arr[i] + used >= 0) { continue; } long long max1 = suf[i] + used; long long tmp = d - max1; if (arr[i] + tmp + used < 0) { cout << "-1"; return 0; } used += tmp; ans++; } cout << ans; } return 0; }
#include <bits/stdc++.h> using namespace std; const long long mod = 1e9 + 7; const int N = 4e5 + 4; int main() { int n, k; scanf("%d", &n); scanf("%d", &k); int a[n]; for (int i = 0; i < n; i++) scanf("%d", &a[i]); int sum = 0; for (int i = 0; i < n; i++) { if (a[i] == 0 and sum < 0) sum = 0; else sum += a[i]; if (sum > k) { printf("%d", -1); return 0; } } int ans = 0; sum = 0; for (int i = 0; i < n; i++) { if (a[i] == 0 and sum < 0) { sum = k; ans++; } sum += a[i]; if (sum > k) sum = k; } printf("%d", ans); return 0; }
#include <bits/stdc++.h> using namespace std; int solve(int n) { int d; cin >> d; vector<int> a(n); for (int i = 0; i < (int)(n); ++i) cin >> a[i]; long long cur = 0; int ans = 0; vector<int> suff_max(n + 1); for (int i = ((int)(n)-1); i >= 0; --i) suff_max[i] = max(0, a[i] + suff_max[i + 1]); for (int i = 0; i < (int)(n); ++i) { cur += a[i]; if (cur > d) return -1; if (a[i] == 0 && cur < 0) { cur = d - suff_max[i + 1], ans++; if (cur < 0) return -1; } } return ans; } int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); int n; while (cin >> n) { auto ans = solve(n); cout << ans << endl; } }
#include <bits/stdc++.h> using namespace std; const long long INF = 1e9 + 123, MAXN = 2e5 + 47; template <class T> istream& operator>>(istream& in, vector<T>& a) { for (auto& i : a) in >> i; return in; } template <class T> ostream& operator<<(ostream& out, vector<T>& a) { for (auto& i : a) out << i << " "; return out; } template <class T, class D> istream& operator>>(istream& in, vector<pair<T, D>>& a) { for (auto& i : a) in >> i.first >> i.second; return in; } template <class T, class D> ostream& operator<<(ostream& out, vector<pair<T, D>>& a) { for (auto& i : a) out << i.first << " " << i.second << endl; return out; } signed main() { setlocale(LC_ALL, "rus"); ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); long long n, d; cin >> n >> d; vector<long long> a(n); cin >> a; vector<long long> pref(n); pref[0] = a[0]; long long ans = 0; vector<long long> suff(n); suff.back() = a.back(); for (long long i = n - 2; i >= 0; --i) suff[i] = max({suff[i + 1] + a[i], suff[i], 0ll, a[i]}); if (pref[0] > d) return cout << -1, 0; for (long long i = 1; i < n; ++i) { pref[i] = pref[i - 1] + a[i]; if (a[i] == 0) { if (pref[i] < 0) { ++ans; pref[i] = d - suff[i]; if (pref[i] < 0) return cout << -1, 0; } } if (pref[i] > d) return cout << -1, 0; } cout << ans; }
#include <bits/stdc++.h> using namespace std; int a[100005]; int s[100005]; int c[100005]; int main() { int n, d; cin >> n >> d; for (int i = 1; i <= n; ++i) { cin >> a[i]; s[i] = s[i - 1] + a[i]; c[i] = s[i]; } int add = 0; for (int i = n - 1; i > 0; --i) { c[i] = max(c[i], c[i + 1]); } int res = 0; int ans = 0; for (int i = 1; i <= n; ++i) { ans += a[i]; if (ans > d) { cout << -1; return 0; } if (ans < 0 && a[i] == 0) { c[i] += add; int t = max(0, min(d - c[i], d - ans)); ans += t; add += t; ++res; if (ans < 0) { cout << -1; return 0; } } } cout << res; return 0; }
#include <bits/stdc++.h> using namespace std; bool db = false; const long long INF = 1e18 + 1; const int maxn = 1e5 + 1; int n, d; int a[maxn]; int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> n >> d; vector<int> v; for (int i = 0; i < n; i++) cin >> a[i]; int ans = 0; int balance = 0; int mx_balance = 0; int x; for (int i = 0; i < n; i++) { x = a[i]; if (x == 0) continue; balance += x; if (balance > d) { ans = -1; break; } mx_balance = max(mx_balance, balance); if (balance < 0 and i + 1 < n and a[i + 1] == 0) { if (ans == 0) { ans++; balance = 0; mx_balance = 0; continue; } if (-balance <= d - mx_balance) { mx_balance += (-balance); balance = 0; } else { balance = 0; mx_balance = 0; ans++; } } } cout << ans << endl; return 0; }
#include <bits/stdc++.h> using namespace std; constexpr long long maxN = 1e5 + 43; long long n, d; vector<long long> a; long long suff[maxN]; long long had_max[maxN]; signed main() { ios_base::sync_with_stdio(false); cin >> n >> d; a.resize(n + 1); for (long long i = 1; i <= n; ++i) cin >> a[i]; long long ans = 0; long long maxx = 0, minn = 0; for (long long i = 1; i <= n; ++i) { if (a[i] != 0) { maxx += a[i]; minn += a[i]; maxx = min(maxx, d); if (minn > d) { cout << -1; return 0; } } else { if (maxx < 0) { maxx = d; minn = 0; ++ans; } else { minn = max((long long)0, minn); } } } cout << ans; return 0; }
#include <bits/stdc++.h> using namespace std; ifstream fin("input.in"); ofstream fout("output.out"); int main() { int n, d, ans = 0, inc = 0; cin >> n >> d; vector<long long> v(n + 1); vector<long long> maxInc(n + 1); for (int i = 1; i <= n; ++i) { int x; cin >> x; v[i] = v[i - 1] + x; if (d < v[i]) { cout << -1; return 0; } } for (int i = n; i > 0; --i) { if (i < n) { maxInc[i] = min(maxInc[i + 1], d - v[i]); } else { maxInc[i] = d - v[i]; } } for (int j = 1; j <= n; ++j) { if (d < v[j] + inc) { cout << -1; return 0; } if (v[j] == v[j - 1] && v[j] + inc < 0) { ans++, inc = maxInc[j]; if (v[j] + inc < 0) { cout << -1; return 0; } } } cout << ans; return 0; }
#include <bits/stdc++.h> using namespace std; template <typename T> inline bool chkmin(T &a, const T &b) { return a > b ? a = b, 1 : 0; } template <typename T> inline bool chkmax(T &a, const T &b) { return a < b ? a = b, 1 : 0; } const int MAX_N = 1e5 + 5; int n; long long d; long long a[MAX_N], rec[MAX_N]; long long add[MAX_N << 2]; long long ma[MAX_N << 2]; void pushUp(int rt) { ma[rt] = max(ma[rt << 1], ma[rt << 1 | 1]); } void pushDown(int rt) { if (add[rt]) { add[rt << 1] += add[rt]; add[rt << 1 | 1] += add[rt]; ma[rt << 1] += add[rt]; ma[rt << 1 | 1] += add[rt]; add[rt] = 0; } } void build(int l, int r, int rt) { add[rt] = 0; if (l == r) { ma[rt] = rec[l]; return; } int m = (l + r) >> 1; build(l, m, rt << 1); build(m + 1, r, rt << 1 | 1); pushUp(rt); } void update(int L, int R, int c, int l, int r, int rt) { if (L <= l && r <= R) { add[rt] += c; ma[rt] += c; return; } pushDown(rt); int m = (l + r) >> 1; if (L <= m) update(L, R, c, l, m, rt << 1); if (m < R) update(L, R, c, m + 1, r, rt << 1 | 1); pushUp(rt); } long long query(int L, int R, int l, int r, int rt) { if (L <= l && r <= R) return ma[rt]; pushDown(rt); int m = (l + r) >> 1; long long ret = -0x7fffffff; if (L <= m) ret = max(ret, query(L, R, l, m, rt << 1)); if (m < R) ret = max(ret, query(L, R, m + 1, r, rt << 1 | 1)); return ret; } int main() { scanf("%d%lld", &n, &d); for (int i = (1), _end_ = (n + 1); i < _end_; ++i) scanf("%lld", &a[i]); for (int i = (1), _end_ = (n + 1); i < _end_; ++i) rec[i] = rec[i - 1] + a[i]; build(1, n, 1); int cnt = 0; bool flag = true; long long mav = query(1, n, 1, n, 1); if (mav > d) flag = false; if (flag) for (int i = (1), _end_ = (n + 1); i < _end_; ++i) { if (a[i] == 0) { long long tmp = query(i, i, 1, n, 1); if (tmp >= 0) continue; cnt++; long long mav = query(i, n, 1, n, 1); if (mav > d) { flag = false; break; } if (tmp + d - mav < 0) { flag = false; break; } update(i, n, d - mav, 1, n, 1); } } if (flag) { printf("%d\n", cnt); } else puts("-1"); return 0; }
#include <bits/stdc++.h> using namespace std; const int N = 1e5 + 10; int a[N]; int main() { bool flag; int n, d; int minn, maxn, sum, y; while (cin >> n >> d) { flag = 0; maxn = 0; minn = 0; y = 0; for (int i = 0; i < n; i++) { cin >> a[i]; if (!flag) { if (a[i] == 0) { if (minn < 0) minn = 0; if (maxn < 0) { y++; minn = 0; maxn = d; } } else { minn += a[i]; maxn += a[i]; maxn = min(d, maxn); if (minn > d) { flag = 1; } } } } if (flag) cout << "-1" << endl; else cout << y << endl; } }
#include <bits/stdc++.h> using namespace std; const long long mod = 1e9 + 7; const int N = 1e5 + 10; int a[N]; long long n, d; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); cin >> n >> d; long long m = 0; for (long long i = 0; i < n; i++) { cin >> a[i]; if (!a[i]) { m++; } } m++; long long maxbal[m]; vector<long long> sum[m]; long long acc[m]; long long i = 0; long long j = 0; while (i != m) { while (a[j] != 0 && j != n) { sum[i].push_back(a[j]); j++; } j++; acc[i] = 0; if (!sum[i].empty()) { acc[i] = accumulate(sum[i].begin(), sum[i].end(), 0); if (acc[i] > d) { cout << -1; return 0; } } i++; } for (long long i = 0; i < m; i++) { for (long long j = 1; j < sum[i].size(); j++) { sum[i][j] += sum[i][j - 1]; } sort(sum[i].begin(), sum[i].end()); if (sum[i].empty() == false && sum[i].back() > d) { cout << -1; return 0; } } for (long long i = m - 2; i >= 0; i--) { maxbal[i] = 0; if (acc[i + 1] > d && i == m - 2) { cout << -1; return 0; } if (i == m - 2) { if (!sum[i + 1].empty() && sum[i + 1].back() <= d && sum[i + 1].back() >= 0) { maxbal[i] = d - sum[i + 1].back(); } else if ((!sum[i + 1].empty() && sum[i + 1].back() < 0) || sum[i + 1].empty()) { maxbal[i] = d; } continue; } if (!sum[i + 1].empty() && sum[i + 1].back() <= d && sum[i + 1].back() >= 0) { if (maxbal[i + 1] - acc[i + 1] > d - sum[i + 1].back()) { maxbal[i] = d - sum[i + 1].back(); } else { maxbal[i] = maxbal[i + 1] - acc[i + 1]; } } else if (!sum[i + 1].empty() && sum[i + 1].back() < 0) { if (maxbal[i + 1] - sum[i + 1].back() < d) { maxbal[i] = maxbal[i + 1] - sum[i + 1].back(); } else { maxbal[i] = d; } } else { maxbal[i] = maxbal[i + 1]; } if (maxbal[i] > d || maxbal[i] < 0) { cout << -1; return 0; } } long long bal = 0; long long cnt = 0; long long k = 0; for (long long i = 0; i < n; i++) { if (a[i] != 0) { bal += a[i]; } else { if (bal < 0) { bal = maxbal[k]; cnt++; } if (bal < 0) { cout << -1; return 0; } k++; } if (bal > d) { cout << -1; return 0; } } cout << cnt; return 0; }
#include <bits/stdc++.h> using namespace std; const long long maxn = 100100, mod = 1e9 + 7, maxa = 3e6 + 100, maxb = 27, base = 737, base2 = 3079, mod2 = 242121643; const long long inf = 2e18 + 13; long long max(long long x, long long y) { return (x > y ? x : y); } long long min(long long x, long long y) { return (x < y ? x : y); } long long ps[maxn]; long long pmx[maxn]; long long a[maxn]; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); long long n, d; cin >> n >> d; for (int i = 0; i < n; i++) { cin >> a[i]; } long long fj = 0; for (int i = 0; i < n; i++) { ps[i] = a[i] + fj; fj = ps[i]; } for (int i = 0; i < n; i++) { if (ps[i] > d) { cout << -1; return 0; } } pmx[n] = -inf; for (int i = n - 1; i >= 0; i--) { pmx[i] = max(pmx[i + 1], ps[i]); } long long tmp = 0; long long ans = 0; for (int i = 0; i < n; i++) { if (a[i] != 0) { continue; } if (ps[i] + tmp < 0) { long long val = ps[i] + tmp; long long mx = d - (pmx[i] + tmp); if (val + mx < 0) { cout << -1; return 0; } tmp += mx; ans++; } } cout << ans << endl; return 0; }
#include <bits/stdc++.h> using namespace std; int n, d, a[100010]; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); cin >> n >> d; int s1 = 0, s2 = 0, ans = 0; for (int i = 1; i <= n; ++i) { cin >> a[i]; if (!a[i]) { s1 = max(s1, 0); if (s2 < 0) s2 = d, ++ans; } else { s1 += a[i]; if (s1 > d) return cout << "-1\n", 0; s2 = min(s2 + a[i], d); } } cout << ans << "\n"; return 0; }
#include <bits/stdc++.h> using namespace std; void pr_init() {} void solve() { long long n, d; cin >> n >> d; long long a[n], cu[n], rg[n]; for (long long i = 0; i < n; i++) cin >> a[i]; cu[0] = a[0]; for (long long i = 1; i < n; i++) cu[i] = a[i] + cu[i - 1]; rg[n - 1] = cu[n - 1]; for (long long i = n - 2; i >= 0; i--) { rg[i] = max(rg[i + 1], cu[i]); } long long c = 0, cn = 0; for (long long i = 0; i < n; i++) { if (a[i] == 0) { if (c >= 0) continue; long long tg = d - rg[i] + (i != 0 ? cu[i - 1] : 0) - c; if (tg < 0 || c + tg < 0) { cout << "-1\n"; return; } c += tg; cn++; continue; } c += a[i]; if (c > d) { cout << "-1\n"; return; } } cout << cn << endl; } int32_t main() { pr_init(); ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); solve(); return 0; }
#include <bits/stdc++.h> using namespace std; int main() { long long n, d; cin >> n >> d; long long a[n]; for (int i = 0; i < n; i++) cin >> a[i]; int ans = 0; long long l = 0, r = 0; for (int i = 0; i < n; i++) { if (a[i] > 0) { if (a[i] + l > d) { cout << -1; return 0; } l = min(d, l + a[i]); r = min(d, r + a[i]); } else if (a[i] == 0) { if (r < 0) { ans++; l = 0, r = d; } else { l = max(l, 0LL); r = max(r, 0LL); } } else { l += a[i]; r += a[i]; } } cout << ans; }
#include <bits/stdc++.h> using namespace std; const long long N = 1e5; long long n, k; int main() { ios_base::sync_with_stdio(false); cin >> n >> k; long long a[n]; for (long long i = 0; i < n; i++) cin >> a[i]; long long curr_bank = 0, ans = 0, mini, flag = 0; for (long long i = 0; i < n; i++) { if (a[i] == 0) { if (curr_bank < 0) { curr_bank = k; ans++; mini = k; } else { mini = min(mini, curr_bank); } continue; } if (curr_bank + a[i] > k) { if (ans == 0) { flag = 1; break; } long long diff = curr_bank + a[i] - k; curr_bank = k; mini -= diff; if (mini < 0) { flag = 1; break; } } else { curr_bank += a[i]; } } if (flag) cout << -1; else cout << ans; }
#include <bits/stdc++.h> using namespace std; int s[101000], f[101000], g[101000], n, m; int ans; int main() { int i, j, k; scanf("%d%d", &n, &m); for (i = 1; i <= n; i++) scanf("%d", &s[i]); g[n] = m; for (i = n - 1; i; i--) g[i] = min(m, min(m, g[i + 1]) - s[i + 1]); for (i = 1; i <= n; i++) { f[i] = f[i - 1] + s[i]; if (s[i] == 0) { if (f[i] < 0) { ans++; f[i] = max(0, g[i]); } } if (f[i] > g[i]) { puts("-1"); return 0; } } cout << ans << endl; return 0; }
#include <bits/stdc++.h> int v[100001], s[100001], max[100001]; int main() { int n, d, i, nr, cr; scanf("%d%d", &n, &d); for (i = 1; i <= n; i++) scanf("%d", &v[i]); nr = 0; for (i = 1; i <= n; i++) { s[i] = s[i - 1] + v[i]; if (s[i] > d) nr = -1; } max[n] = s[n]; for (i = n - 1; i > 0; i--) { max[i] = max[i + 1]; if (s[i] > max[i]) max[i] = s[i]; } cr = 0; if (nr != -1) { for (i = 1; i <= n && nr != -1; i++) if (nr != -1 && v[i] == 0 && s[i] + cr < 0) { if (d - max[i] - cr >= 0) cr += (d - max[i] - cr); nr++; if (s[i] + cr < 0) nr = -1; } } printf("%d\n", nr); return 0; }
#include <bits/stdc++.h> using namespace std; int main() { int n, d; cin >> n >> d; vector<int> a(n), pre(n), mxpsuf(n, 0); int mx = 0; for (int i = 0; i < n; i++) { cin >> a[i]; (i - 1) > -1 ? pre[i] = pre[i - 1] + a[i] : pre[i] = a[i]; mx = max(mx, pre[i]); } if (mx > d) { cout << -1 << endl; return 0; } for (int i = n - 1; i >= 0; i--) { mxpsuf[i] = (i + 1 < n) ? max(pre[i], mxpsuf[i + 1]) : pre[i]; } int ans = 0; long long cnt = 0; for (int i = 0; i < n; i++) { if (a[i] == 0) { if (pre[i] + cnt >= 0) { continue; } else { int inc = d - (mxpsuf[i - 1] + cnt); if (inc + pre[i] + cnt >= 0) { cnt += inc; ans++; } else { cout << -1 << endl; return 0; } } } } cout << ans << endl; return 0; }
#include <bits/stdc++.h> using namespace std; int n, d; int main() { scanf("%d%d", &n, &d); int bal = 0; int ans = 0; long long sim = 0; for (int i = 1; i <= n; i++) { int x; scanf("%d", &x); if (x == 0) { if (bal < 0) { ans++; bal = d; sim += d; } else { sim = min((long long)bal, sim); } } else { bal += x; if (bal > d) sim -= bal - d, bal = d; if (sim < 0) { ans = -1; break; } } } printf("%d\n", ans); return 0; }
#include <bits/stdc++.h> using namespace std; long long n, d, c, flag, minn, maxx, minn2, sum, ans, flag1, a[101010]; int main() { flag1 = 1; cin >> n >> d; for (int i = (0); i < (n); i++) { cin >> a[i]; sum += a[i]; minn = min(minn, sum); maxx = max(maxx, sum); if (a[i] == 0) { if ((maxx - sum) > d || (flag1 && sum < 0)) { ans++; minn = 0; maxx = 0; sum = 0; flag1 = 0; } } } sum = 0; for (int i = 0; i < n && (!flag); i++) { sum += a[i]; if (!a[i] && sum < 0) sum = 0; if (sum > d) { flag = 1; } } if (flag == 1) { cout << -1 << endl; return 0; } cout << ans << endl; return 0; }
#include <bits/stdc++.h> using namespace std; using lli = long long int; using ll = lli; using llu = unsigned long long int; using pii = tuple<lli, lli>; using piii = tuple<lli, lli, lli>; using vi = vector<lli>; using vii = vector<pii>; using viii = vector<piii>; using vvi = vector<vi>; using vvii = vector<vii>; using vviii = vector<viii>; using vb = vector<bool>; using vvb = vector<vb>; int main() { int n, d; scanf("%d%d", &n, &d); vi a(n); for (lli i = 0; i < (lli)(n); ++i) scanf("%lld", &a[i]); vi ca(n + 1); for (lli i = (lli)(1); i < (lli)(n + 1); ++i) ca[i] = ca[i - 1] + a[i - 1]; vi mca(n + 1); mca[n] = ca[n]; for (lli i = (lli)(n)-1; i >= (lli)(0); --i) mca[i] = max(ca[i], mca[i + 1]); int ans = 0; lli S = 0, add = 0; for (lli i = 0; i < (lli)(n); ++i) { if (a[i] == 0 && S < 0) { lli v = d - (mca[i] + add); S += v; add += v; if (S < 0) { puts("-1"); return 0; } ans++; } S += a[i]; if (S > d) { puts("-1"); return 0; } } printf("%d\n", ans); }
#include <bits/stdc++.h> using namespace std; long long st[100000 + 1][25 + 1]; long long lg[100000 + 1]; long long maxinrange(long long L, long long R) { int j = lg[R - L + 1]; int minimum = max(st[L][j], st[R - (1 << j) + 1][j]); return minimum; } int main() { lg[1] = 0; for (int i = 2; i <= 100000; i++) lg[i] = lg[i / 2] + 1; long long n, d; scanf("%lld %lld", &n, &d); long long arr[n + 1]; for (long long i = 1; i <= n; i++) scanf("%lld", &arr[i]); long long csum[n + 1]; csum[0] = 0; for (long long i = 1; i <= n; i++) csum[i] = csum[i - 1] + arr[i]; for (int i = 1; i <= n; i++) st[i][0] = csum[i]; for (int j = 1; j <= 25; j++) for (int i = 1; i + (1 << j) <= n + 1; i++) st[i][j] = max(st[i][j - 1], st[i + (1 << (j - 1))][j - 1]); vector<long long> idx; for (long long i = 1; i <= n; i++) if (csum[i] < 0 && arr[i] == 0) idx.push_back(i); if (idx.size() == 0) { if (maxinrange(1, n) > d) cout << -1; else cout << 0; exit(0); } vector<long long> maxgaps; if (idx[0] == 0) maxgaps.push_back(-1000000000000000); else maxgaps.push_back(maxinrange(0, idx[0] - 1)); int curr = 1; while (curr != (long long)(idx).size()) { if (idx[curr] - idx[curr - 1] == 1) maxgaps.push_back(-1000000000000000); else { long long val = maxinrange(idx[curr - 1] + 1, idx[curr - 1] - 1); maxgaps.push_back(val); } curr++; } if (n == idx[curr - 1]) maxgaps.push_back(-1000000000000000); else maxgaps.push_back(maxinrange(idx[curr - 1] + 1, n)); if (maxgaps[0] > d) { cout << -1; exit(0); } long long upd = 0; long long minc[(long long)(idx).size()]; minc[(long long)(idx).size() - 1] = csum[idx[(long long)(idx).size() - 1]]; for (int i = (long long)(idx).size() - 2; i >= 0; --i) minc[i] = min(minc[i + 1], csum[idx[i]]); long long minupd[(long long)(maxgaps).size()]; minupd[(long long)(maxgaps).size() - 1] = d - maxgaps[(long long)(maxgaps).size() - 1]; for (int i = (long long)(maxgaps).size() - 2; i >= 0; i--) minupd[i] = min(minupd[i + 1], d - maxgaps[i]); curr = 0; int ans = 0; while (curr != (long long)(idx).size()) { if (csum[idx[curr]] + upd >= 0) { curr++; continue; } long long maxupdate = 0; maxupdate = minc[curr] + upd; maxupdate = -maxupdate; long long lowestgap = 1000000000000000; lowestgap = minupd[curr + 1] - upd; maxupdate = min(maxupdate, lowestgap); if (csum[idx[curr]] + upd + maxupdate < 0) { cout << -1; exit(0); } upd += maxupdate; curr++; ans++; } cout << ans; return 0; }
#include <bits/stdc++.h> using namespace std; const int N = 2e5 + 5; void solve() { long long n, d, a[N]; cin >> n >> d; long long mx = 0, mn = 0, ans = 0; for (int i = 0; i < (int)n; ++i) { cin >> a[i]; mx += a[i]; mn += a[i]; if (a[i] == 0) { if (mx < 0) { ans += 1; mx = d; } mn = max(mn, 0ll); } mx = min(mx, d); if (mn > d) { cout << -1 << "\n"; return; } } cout << ans << "\n"; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int t = 1; while (t--) { solve(); } }
#include <bits/stdc++.h> using namespace std; const long long int INF = 1000000007; const int N = 1000000 + 7; long long int trans[N]; long long int ca[N]; long long int maxb[N]; int main() { std::ios::sync_with_stdio(false); cin.tie(NULL); int n; long long int d; cin >> n >> d; for (int i = 0; i < n; i++) cin >> trans[i]; ca[0] = trans[0]; for (int i = 1; i < n; i++) ca[i] = ca[i - 1] + trans[i]; maxb[n] = LLONG_MIN / 2; for (int i = n - 1; i >= 0; i--) maxb[i] = max(ca[i], maxb[i + 1]); long long int currentAdd = 0; int ans = 0; for (int i = 0; i < n; i++) { ca[i] += currentAdd; if (ca[i] > d) { cout << -1 << endl; return 0; } if (trans[i] == 0) { if (ca[i] < 0) { long long int all = d - (maxb[i + 1] + currentAdd); if (ca[i] + all > d) { all = d - ca[i]; } ca[i] += all; currentAdd += all; if (ca[i] < 0) { cout << -1 << endl; return 0; } ans++; } } } cout << ans << endl; return 0; }
#include <bits/stdc++.h> using namespace std; int main() { long long n, d; cin >> n >> d; long long arr[n + 1], day[n + 1]; day[0] = 0; for (long long i = 1; i <= n; i++) { cin >> arr[i]; day[i] = day[i - 1] + arr[i]; } long long mx[n + 1]; mx[n] = day[n]; for (long long i = n - 1; i >= 1; i--) { mx[i] = max(mx[i + 1], day[i]); } long long ans = 0, add = 0; for (long long i = 1; i <= n; i++) { if (arr[i] == 0 and day[i] + add < 0) { add += max(0 - day[i] - add, d - add - mx[i]); ans++; } if (day[i] + add > d) { cout << -1 << endl; return 0; } } cout << ans << endl; }
#include <bits/stdc++.h> using namespace std; class A { public: void solve() { int n; cin >> n; vector<int> a(n); for (int i = 0; i < n; i++) cin >> a[i]; bool ok = true; int A = 1, B = 2, C = 3; for (int i = 0; i < n; i++) { if (a[i] == A) swap(B, C); else if (a[i] == B) swap(A, C); else { ok = false; break; } } if (ok) cout << "YES" << endl; else cout << "NO" << endl; } }; class B { public: void solve() { long long n; cin >> n; vector<long long> div; div.push_back(1); if (n != 1) div.push_back(n); for (long long i = 2; i <= sqrt(n); i++) { if (n % i == 0) { div.push_back(i); if (i != n / i) div.push_back(n / i); } } long long res = 1; for (long long d : div) { for (int k = 1; k < 32 && ((1 << k) - 1 <= d); k++) { if (d == ((1 << k) - 1) * (1 << (k - 1))) { res = max(res, d); } } } cout << res << endl; } }; class C { public: vector<int> fa; vector<long long> cost; int find(int x) { if (x != fa[x]) return fa[x] = find(fa[x]); return x; } void merge(int x, int y) { int fx = find(x); int fy = find(y); if (fx != fy) { fa[fx] = fy; cost[fy] = min(cost[fy], cost[fx]); } } void solve() { long long n, m; cin >> n >> m; vector<long long> c(n + 1); fa = vector<int>(n + 1); cost = vector<long long>(n + 1, 0); for (int i = 1; i <= n; i++) { cin >> c[i]; fa[i] = i; cost[i] = c[i]; } for (int i = 0; i < m; i++) { int u, v; cin >> u >> v; merge(u, v); } unordered_set<long long> st; for (int i = 1; i <= n; i++) { st.insert(find(i)); } long long res = 0; for (auto i : st) res += cost[i]; cout << res << endl; } }; class D { public: void solve() { long long n, d; cin >> n >> d; vector<long long> a(n); for (int i = 0; i < n; i++) cin >> a[i]; vector<long long> csb(n, 0); vector<long long> sum(n, 0); for (int i = 0; i < n; i++) { sum[i] = (i > 0 ? sum[i - 1] : 0) + a[i]; if (sum[i] > d) { cout << -1 << endl; return; } } csb[n - 1] = d - sum[n - 1]; for (int i = n - 2; i >= 0; i--) { csb[i] = min(d - sum[i], csb[i + 1]); } long long cnt = 0, res = 0; for (int i = 0; i < n; i++) { long long cur = sum[i] + cnt; if (a[i] == 0 && cur < 0) { if (csb[i] - cnt < abs(cur)) { cout << -1 << endl; return; } cnt += csb[i] - cnt; res++; } } cout << res << endl; } }; int main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); D ans; ans.solve(); return 0; }
#include <bits/stdc++.h> using namespace std; const int maxn = 1e5 + 5; int a[maxn]; int pre[maxn]; int use[maxn]; int main() { int n, d; cin >> n >> d; pre[0] = 0; for (int i = 1; i <= n; i++) { cin >> a[i]; pre[i] = pre[i - 1] + a[i]; } use[n] = pre[n]; for (int i = n - 1; i >= 1; i--) { use[i] = max(use[i + 1], pre[i]); } long long add = 0; long long ans = 0; int flag = 1; for (int i = 1; i <= n; i++) { if (a[i] == 0) { if (add + pre[i] < 0) { add += d - (use[i] + add); ans++; } if (add + pre[i] < 0 && flag) { flag = 0; cout << "-1" << endl; } } else { if (add + pre[i] > d && flag) { flag = 0; cout << "-1" << endl; } } } if (flag) cout << ans << endl; return 0; }
#include <bits/stdc++.h> using namespace std; long long A[100010], B[100010], maxi[100010]; int main() { ios::sync_with_stdio(false); cin.tie(NULL); int n, d; cin >> n >> d; for (int i = 0; i < n; i++) cin >> A[i]; B[0] = A[0]; if (A[0] > d) { cout << -1 << '\n'; return 0; } for (int i = 1; i < n; i++) { B[i] += B[i - 1] + A[i]; if (B[i] > d) { cout << -1 << '\n'; return 0; } } maxi[n - 1] = B[n - 1]; for (int i = n - 2; i >= 0; i--) { maxi[i] = max(maxi[i + 1], B[i]); } long long cur = 0, add = 0; int ans = 0; for (int i = 0; i < n; i++) { if (A[i] == 0) { if (cur < 0) { long long val = maxi[i] + add; long long rem = d - val; if (rem > 0 && cur + rem >= 0) { add += rem; ans++; cur += rem; } else { cout << -1 << '\n'; return 0; } } } else { cur += A[i]; } } cout << ans << '\n'; }
#include <bits/stdc++.h> using namespace std; using namespace chrono; template <typename Arg1> void __f(const char* name, Arg1&& arg1) { cout << name << " : " << arg1 << "\n"; } template <typename Arg1, typename... Args> void __f(const char* names, Arg1&& arg1, Args&&... args) { const char* comma = strchr(names + 1, ','); cout.write(names, comma - names) << " : " << arg1 << " | "; __f(comma + 1, args...); } long long int advceil(long long int num, long long int den) { return (num % den == 0 ? num / den : num / den + 1); } long long int lstbt(long long int val) { long long int msk = val & (val - 1); return log2(val ^ msk); } long long int modmul(long long int a, long long int b) { if (a < 1e9 and b < 1e9) return (a * b) % 1000000007; a %= 1000000007; b %= 1000000007; long long int res = 0; while (b > 0) { if (b & 1) res = (res + a) % 1000000007; a = (a * 2) % 1000000007; b /= 2; } return res; } long long int modexpo(long long int a, long long int b) { long long int res = 1; while (b > 0) { if (b & 1) res = modmul(res, a); a = modmul(a, a); b /= 2; } return res; } long long int gcd(long long int a, long long int b) { return a == 0 ? b : gcd(b % a, a); } vector<long long int> CALCfactor(long long int n) { vector<long long int> factor(n + 2, 0); for (long long int i = 4; i <= n; i += 2) factor[i] = 2; for (long long int j = 3; j <= n; j += 2) { if (factor[j]) continue; for (long long int i = 2 * j; i <= n; i += j) factor[i] = j; } return factor; } vector<long long int> CALCprimeNUM(long long int n) { vector<long long int> factor = CALCfactor(n); vector<long long int> primeNUM; primeNUM.reserve(n + 2); for (long long int i = 2; i <= n; i++) if (!factor[i]) primeNUM.push_back(i); return primeNUM; } vector<long long int> CALCprimeFACTOR(long long int n) { vector<long long int> factor = CALCfactor(n); vector<long long int> ans; while (factor[n] != 0) { ans.push_back(factor[n]); n /= factor[n]; } if (n > 1) ans.push_back(n); return ans; } vector<long long int> unique(vector<long long int> x) { sort(x.begin(), x.end()); set<long long int> s; vector<long long int> ans; ans.reserve(x.size()); for (auto elem : x) s.insert(elem); for (auto elem : s) ans.push_back(elem); return ans; } pair<vector<long long int>, vector<long long int> > getFact(long long int n) { vector<long long int> fact(n + 1, 1), invfact(n + 1, 1); for (long long int i = 1; i <= n; i++) fact[i] = (i * (fact[i - 1])) % 1000000007; for (long long int i = 1; i <= n; i++) invfact[i] = (modexpo(i, 1000000007 - 2) * invfact[i - 1]) % 1000000007; return {fact, invfact}; } void compress(vector<long long int>& arr, long long int n) { map<long long int, vector<long long int> > pos; for (long long int i = 1; i <= n; i++) pos[arr[i]].push_back(i); long long int cnt = 1; for (auto itr : pos) { for (auto elem : itr.second) arr[elem] = cnt; cnt++; } } bool rcomp(long long int a, long long int b) { return a > b; } void solve() { long long int n, k; cin >> n >> k; vector<long long int> arr(n + 2, 0); for (long long int i = 1; i <= n; i++) cin >> arr[i]; vector<long long int> pref(n + 2, 0), suff(n + 2, -1e12); for (long long int i = 1; i <= n; i++) { pref[i] = pref[i - 1] + arr[i]; if (pref[i] > k) { cout << "-1" << "\n"; return; } } for (long long int i = n + 1 - 1; i >= 1; i--) suff[i] = max(suff[i + 1], pref[i]); vector<long long int> zeropos; for (long long int i = 1; i <= n; i++) if (arr[i] == 0) zeropos.push_back(i); long long int ans = 0, profit = 0; vector<long long int> set; for (auto elem : zeropos) { if (pref[elem] + profit >= 0) continue; ans++; long long int profithere = abs(pref[elem] + profit); long long int maxval = max(0ll, k - suff[elem] - profit); if (maxval >= profithere) profit += maxval; else { cout << "-1" << "\n"; return; } } cout << ans << "\n"; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); auto start1 = high_resolution_clock::now(); solve(); auto stop1 = high_resolution_clock::now(); auto duration = duration_cast<microseconds>(stop1 - start1); }
#include <bits/stdc++.h> using namespace std; int n, d, i, a, ans, l, r; int main() { ios_base::sync_with_stdio(0); cin.tie(0), cout.tie(0); cin >> n >> d; l = r = 0; for (i = 1; i <= n; i++) { cin >> a; if (a) { l += a; r += a; if (l > d) { return puts("-1"); } if (r > d) r = d; } else { if (l < 0) l = 0; if (r < 0) r = d, ans++; } } return !(cout << ans); }
#include <bits/stdc++.h> using namespace std; const int INF = 0x3f3f3f3f; const int maxn = 1e5 + 5; int a[maxn], p[maxn], c[maxn]; int d1[maxn][19], log_2[maxn]; int n, d; void RMQ_init() { log_2[1] = 0; for (int i = 2; i <= n; i++) { log_2[i] = log_2[i - 1]; if ((1 << log_2[i] + 1) == i) ++log_2[i]; } for (int i = 0; i < n; i++) d1[i][0] = p[i]; for (int j = 1; (1 << j) <= n; j++) for (int i = 0; i + (1 << j) - 1 < n; i++) { d1[i][j] = max(d1[i][j - 1], d1[i + (1 << (j - 1))][j - 1]); } } int MAX(int l, int r) { int k = log_2[r - l + 1]; return max(d1[l][k], d1[r - (1 << k) + 1][k]); } int main() { ios::sync_with_stdio(0); cin >> n >> d; bool ok = 1, noans = 0; for (int i = 0; i < n; i++) { cin >> a[i]; p[i] = a[i]; if (i) p[i] += p[i - 1]; if (a[i] == 0 && p[i] < 0) ok = 0; if (p[i] > d) noans = 1; } if (noans) { cout << -1 << endl; return 0; } RMQ_init(); for (int i = 0; i < n; i++) { c[i] = d - MAX(i, n - 1); } ok = 1; int pls = 0, cnt = 0; for (int i = 0; i < n; i++) { if (a[i] == 0 && p[i] + pls < 0) { if (c[i] > pls) { pls += c[i] - pls; cnt++; } if (p[i] + pls < 0) { ok = 0; break; } } } if (ok) { cout << cnt << endl; } else { cout << -1 << endl; } return 0; }
#include <bits/stdc++.h> using namespace std; long long n, m, k; long long int temp1, t, mini = 1000000007, a[100008]; long long fastpow(long long int a, long long int b) { if (b == 0) return 1ll; long long ret = fastpow(a, b / 2); ret = (ret * ret) % 1000000007; ret = (ret + 1000000007) % 1000000007; if (b % 2 == 1) ret = (ret * a) % 1000000007; ret = (ret + 1000000007) % 1000000007; return ret; } vector<long long int> vec[100008]; long long int visited[100008]; void dfs(long long int node) { visited[node] = 1; mini = min(a[node], mini); for (int i = 0; i < vec[node].size(); i++) { if (!visited[vec[node][i]]) { dfs(vec[node][i]); } } } map<long long int, long long int> mymap; long long int pre[100008], d; long long int findmax(long long int lo, long long int hi, long long int offset) { long long int low = lo, high = hi; while (low < high) { long long int mid = (high + low) / 2; long long int check = d - mid + offset; auto it = mymap.upper_bound(check); if (it != mymap.end()) { high = mid - 1; } else { low = mid + 1; } } long long int temp = d - low + offset; auto tt = mymap.upper_bound(temp); if (tt != mymap.end()) { if (low == 0) return -1; return low - 1; } else { return low; } } int main() { bool flag = 0; long long int ans = 0; cin >> n >> d; for (int i = 1; i <= n; i++) cin >> a[i]; pre[0] = 0; for (int i = 1; i <= n; i++) { pre[i] = pre[i - 1] + a[i]; auto it = mymap.find(pre[i]); if (it != mymap.end()) { mymap[pre[i]]++; } else { mymap[pre[i]] = 1; } } long long int cur = 0; for (int i = 1; i <= n; i++) { auto it = mymap.find(pre[i]); if ((it->second) == 1) { mymap.erase(it); } else { (it->second)--; } if (a[i]) { cur += a[i]; } else { if (cur < 0) { long long int res = findmax(0, d, pre[i]); if (res != -1) { cur = res; ans++; } else { flag = 1; break; } } } if (cur > d) { flag = 1; break; } } if (!flag) cout << ans << endl; else cout << -1 << endl; return 0; }
#include <bits/stdc++.h> using namespace std; int A[100010]; int main() { ios_base::sync_with_stdio(false); int n, d; cin >> n >> d; long long amount = 0; int res = 0; for (int i = 0; i < n; ++i) { cin >> A[i]; if (A[i] == 0) { if (amount < 0) amount = 0; } amount += A[i]; if (amount > d) res = -1; } if (res == -1) { cout << res << "\n"; return 0; } amount = 0; long long pico = 0; res = 0; for (int i = 0; i < n; ++i) { if (A[i] == 0) { if (amount < 0) { if (res > 0 && d - pico >= -amount) { pico -= amount; amount = 0; } else { amount = 0; pico = 0; res++; } } } amount += A[i]; pico = max(amount, pico); } cout << res << "\n"; return 0; }
#include <bits/stdc++.h> using namespace std; int main() { std::ios::sync_with_stdio(false), cin.tie(0); int n, d, t, now = 0, cnt = 0, flag = 1, balance = 0; cin >> n >> d; for (int i = 1; i <= n; i++) { cin >> t; if (t > 0) { now += t; if (now > d) { flag = 0; } } else if (t == 0) { if (now < 0) { if (now + balance < 0) { cnt++; balance = d; } else { balance = balance + now; } now = 0; } } else { now += t; } if (now + balance > d) balance = d - now; } if (!flag) cout << -1 << endl; else cout << cnt << endl; return 0; }
#include <bits/stdc++.h> using namespace std; long long n, m, k; int temp1, t, mini = 1000000007, a[100008]; long long fastpow(long long int a, long long int b) { if (b == 0) return 1ll; long long ret = fastpow(a, b / 2); ret = (ret * ret) % 1000000007; ret = (ret + 1000000007) % 1000000007; if (b % 2 == 1) ret = (ret * a) % 1000000007; ret = (ret + 1000000007) % 1000000007; return ret; } vector<long long int> vec[100008]; long long int visited[100008]; void dfs(long long int node) { visited[node] = 1; mini = min(a[node], mini); for (int i = 0; i < vec[node].size(); i++) { if (!visited[vec[node][i]]) { dfs(vec[node][i]); } } } map<int, int> mymap; int pre[100008], d; int findmax(int lo, int hi, int offset) { int low = lo, high = hi; while (low < high) { int mid = (high + low) / 2; int check = d - mid + offset; auto it = mymap.upper_bound(check); if (it != mymap.end()) { high = mid - 1; } else { low = mid + 1; } } int temp = d - low + offset; auto tt = mymap.upper_bound(temp); if (tt != mymap.end()) { if (low == 0) return -1; return low - 1; } else { return low; } } int main() { bool flag = 0; long long int ans = 0; cin >> n >> d; for (int i = 1; i <= n; i++) cin >> a[i]; pre[0] = 0; for (int i = 1; i <= n; i++) { pre[i] = pre[i - 1] + a[i]; auto it = mymap.find(pre[i]); if (it != mymap.end()) { mymap[pre[i]]++; } else { mymap[pre[i]] = 1; } } int cur = 0; for (int i = 1; i <= n; i++) { auto it = mymap.find(pre[i]); if ((it->second) == 1) { mymap.erase(it); } else { (it->second)--; } if (a[i]) { cur += a[i]; } else { if (cur < 0) { int res = findmax(0, d, pre[i]); if (res != -1) { cur = res; ans++; } else { flag = 1; break; } } } if (cur > d) { flag = 1; break; } } if (!flag) cout << ans << endl; else cout << -1 << endl; return 0; }