voidsolve(){ int n; cin >> n; vector<int> data(n), dp(n), dr(n); for (auto &i: data) cin >> i; dp[0] = data[0] >= 1 ? data[0] : 1; dr[0] = data[0] >= 1 ? -1 : 0; for (int i = 1; i < n; ++i) { dp[i] = dp[i - 1] + data[i]; dr[i] = -1; for (int j = i; j >= 1; --j) if (dp[j - 1] + (i - j + 1) * (i - j + 1) > dp[i]) { dp[i] = dp[j - 1] + (i - j + 1) * (i - j + 1); dr[i] = j; } if ((i + 1) * (i + 1) > dp[i]) { dr[i] = 0; dp[i] = (i + 1) * (i + 1); } } vector<pair<int, int>> ans; ans.reserve(2e5);
auto zero = [&](int l, int r) { bool flag = true; for (int i = l; i <= r; ++i) if (data[i] == 0) flag = false; if (flag) ans.emplace_back(l, r); else { ans.emplace_back(l, r); ans.emplace_back(l, r); } for (int i = l; i <= r; ++i) data[i] = 0; }; function<void(int, int)> dfs = [&](int l, int r) { if (l == r) { if (data[l] == 0) return; ans.emplace_back(l, l); return; } dfs(l, r - 1); ans.emplace_back(l, r); ans.emplace_back(l, r - 1); data[r] = r - l; dfs(l, r - 1); };
auto f = [&](int l, int r) { zero(l, r); dfs(l, r); ans.emplace_back(l, r); }; vector<pair<int, int>> lr; for (int i = n - 1; i >= 0; --i) { if (dr[i] == -1) continue; f(dr[i], i); i = dr[i]; }