This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
// In the Name of Allah
#include<bits/stdc++.h>
using namespace std;
#pragma GCC optimize("Ofast,unroll-loops,fast-math")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4.1,avx,avx2,sse4.2,popcnt,tune=native")
typedef long long ll;
#define F first
#define S second
#define pii pair<int, int>
#define pb push_back
#define pp pop_back
#define all(x) x.begin(), x.end()
const int N = 1e5 + 3, M = 17, inf = 1e9 + 7;
int a[N], n, dp[N], f[N][M][M];
void ip() {
cin >> n;
for (int i = 1; i <= n; i++)
cin >> a[i];
}
void solve() {
memset(f, 63, sizeof f);
for (int i = 1; i <= n; ++i) {
if (a[i] >= M) {
cout << -1;
return;
}
f[i][0][a[i]] = 0;
if (a[i])
f[i][0][a[i] - 1] = 1;
for (int j = 1; i - (1 << j) >= 0; ++j) {
int l = i - (1 << (j - 1));
for (int p = 0; p < M - 1; ++p)
f[i][j][p] = min({f[l][j - 1][p] + f[i][j - 1][p], f[l][j - 1][p + 1] + f[i][j - 1][p + 1] + 1, inf});
f[i][j][M - 1] = min(f[l][j - 1][M - 1] + f[i][j - 1][M - 1], inf);
}
}
for (int i = 1; i <= n; i++) {
dp[i] = inf;
for (int j = 0; i - (1 << j) >= 0; ++j)
dp[i] = min(dp[i], f[i][j][0] + dp[i - (1 << j)]);
}
cout << ((dp[n] >= inf)? -1 : dp[n]);
}
int main() {
ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
ip(), solve();
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |