#include <bits/stdc++.h>
using namespace std;
int64_t longest_path(vector<int64_t> &A) {
int64_t n = A.size();
const int64_t INF = INT64_MAX;
vector<int64_t> B(n + 1, INF);
B[0] = -INF;
for (int64_t i = 0; i < n; ++i) {
int64_t j = upper_bound(B.begin(), B.end(), A[i]) - B.begin();
if (B[j-1] <= A[i] && A[i] <= B[j])
B[j] = A[i];
}
int64_t length = 0;
for (int64_t i = 0; i <= n; ++i) {
if (B[i] < INF)
length = i;
}
return length;
}
vector<int64_t> tab;
vector<int64_t> A;
vector<int64_t> B;
int64_t n, liczba, suma = 0;
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> n;
for (int64_t i = 0; i < n; ++i)
{
cin >> liczba;
suma += liczba;
if (suma >= 0)
A.push_back(suma);
}
for (auto i : A)
{
if (i <= A[A.size() - 1])
{
B.push_back(i);
}
}
if (suma < 0)
cout << -1;
else
{
int64_t droga = longest_path(B);
cout << int64_t((n - droga));
}
}
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 | #include <bits/stdc++.h> using namespace std; int64_t longest_path(vector<int64_t> &A) { int64_t n = A.size(); const int64_t INF = INT64_MAX; vector<int64_t> B(n + 1, INF); B[0] = -INF; for (int64_t i = 0; i < n; ++i) { int64_t j = upper_bound(B.begin(), B.end(), A[i]) - B.begin(); if (B[j-1] <= A[i] && A[i] <= B[j]) B[j] = A[i]; } int64_t length = 0; for (int64_t i = 0; i <= n; ++i) { if (B[i] < INF) length = i; } return length; } vector<int64_t> tab; vector<int64_t> A; vector<int64_t> B; int64_t n, liczba, suma = 0; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> n; for (int64_t i = 0; i < n; ++i) { cin >> liczba; suma += liczba; if (suma >= 0) A.push_back(suma); } for (auto i : A) { if (i <= A[A.size() - 1]) { B.push_back(i); } } if (suma < 0) cout << -1; else { int64_t droga = longest_path(B); cout << int64_t((n - droga)); } } |
English