This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
//#pragma GCC optimize("Ofast,O3,unroll-loops")
//#pragma GCC target("avx2")
#include <bits/stdc++.h>
using namespace std;
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define bpc(x) __builtin_popcount(x)
#define bpcll(x) __builtin_popcountll(x)
#define MP make_pair
//#define endl '\n'
mt19937 rng(chrono::high_resolution_clock::now().time_since_epoch().count());
typedef long long ll;
const int MOD = 1e9 + 7;
const int N = 1e6 + 3e2;
int a[N];
void solve(){
    int n;
    cin >> n;
    set<int> zeros;
    for (int i = 1; i <= n; i++){
        cin >> a[i];
        if (a[i] == 0) zeros.insert(i);
    }
    vector<pair<int, int>> segments;
    int k = 10;
    while (k--){
        if (zeros.size() == n) break;
        int l = 1, r = 1 << k;
        while (r <= n){
            auto it = zeros.lower_bound(l);
            bool ok = (it == zeros.end() || *it > r);
            for (auto [sl, sr] : segments){
                if (max(l, sl) > min(r, sr)) continue;
                if (abs(l - sl) % min(r - l + 1, sr - sl + 1) != 0) ok = false;
            }
            if (ok){
                segments.emplace_back(l, r);
                for (int i = l; i <= r; i++){
                    a[i]--;
                    if (a[i] == 0) zeros.insert(i);
                }
            }
            ++l, ++r;
        }
    }
    if (zeros.size() == n){
        cout << segments.size() << endl;
    } else {
        cout << -1 << endl;
    }
}
int main(){
    clock_t startTime = clock();
    ios_base::sync_with_stdio(false);
#ifdef LOCAL
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#endif
    int test_cases = 1;
//    cin >> test_cases;
    for (int test = 1; test <= test_cases; test++){
        // cout << (solve() ? "YES" : "NO") << endl;
        solve();
    }
#ifdef LOCAL
    cerr << "Time: " << int((double) (clock() - startTime) / CLOCKS_PER_SEC * 1000) << " ms" << endl;
#endif
    return 0;
}
Compilation message (stderr)
Main.cpp: In function 'void solve()':
Main.cpp:37:26: warning: comparison of integer expressions of different signedness: 'std::set<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
   37 |         if (zeros.size() == n) break;
      |             ~~~~~~~~~~~~~^~~~
Main.cpp:61:22: warning: comparison of integer expressions of different signedness: 'std::set<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
   61 |     if (zeros.size() == n){
      |         ~~~~~~~~~~~~~^~~~
Main.cpp: In function 'int main()':
Main.cpp:70:13: warning: unused variable 'startTime' [-Wunused-variable]
   70 |     clock_t startTime = clock();
      |             ^~~~~~~~~| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... |