---
id: 2157
lang: en
translation_of: index.md
source_sha256: 853ab55b4b34249931f4d5ba695e5c410b043ff02c0e4273f3567758a44eadf7
title: "Codeforces 933E: A Preponderant Reunion — Solution"
description: "Relaxing the operation to subtract any value instead of the minimum turns the problem into an interval DP, which then reduces to a linear DP."
tags: ["OI","DP"]
translated: 2026-11-27
translator: Claude Opus 5.5
translator_model: claude-opus-5-5
translation_notes: []
---

## Main Idea

I thought about it for ages and still had no idea how to characterize the state; later I found it could be turned into a generalized problem, and then it was much easier to understand.

Consider subtracting not strictly the minimum but an arbitrary value, while still reaching the result the problem asks for. This problem certainly can't be better, and its optimal solution can directly serve as the optimal solution to this problem.

<!--more-->

If that's the case, then we can first try an interval DP. Define $dp[l][r]$ so that this whole interval is cleared and the endpoints are kept; then construct $c_l = p_l, c_i = \max(p_i - c_{i - 1}, 0)$, and we get:

$$
\begin{aligned} f[l][r] &= \sum_{i = l}^{r - 2} c_i + c_{r - 1} + c_r \\ &= f[l][r - 2] + \max(p_r - c_{r - 1}, 0) + c_{r - 1} \\ &= f[l][r - 2] + \max(p_r, c_{r - 1}) \\ &\leq f[l][r - 2] + p_r \\ &= f[l][r - 2] + f[r][r] \end{aligned}
$$

So we can use this to do a linear DP directly, go through the different lengths case by case, and then work backwards.

## Code

```cpp
// CF933E.cpp
#include <bits/stdc++.h>

using namespace std;

const int MAX_N = 3e5 + 200;

typedef long long ll;

int n, ai[MAX_N], pi[MAX_N];
ll f[MAX_N];
bool opt[MAX_N];
vector<int> ans;

int process(int pos)
{
    int val = min(ai[pos], ai[pos + 1]);
    if (val)
        ans.push_back(pos);
    ai[pos] -= val, ai[pos + 1] -= val;
    return val;
}

int main()
{
    scanf("%d", &n);
    for (int i = 1; i <= n; i++)
    {
        scanf("%d", &ai[i]);
        ll single = f[max(0, i - 2)] + ai[i], binary = f[max(0, i - 3)] + max(ai[i], ai[i - 1]);
        f[i] = min(single, binary), opt[i] = f[i] != single;
    }
    int cnt = 0;
    for (int i = min(f[n - 1], f[n]) == f[n - 1] ? n - 1 : n; i >= 1; i -= (opt[i] == true) + 2)
        pi[++cnt] = i;
    reverse(pi + 1, pi + 1 + cnt);
    for (int i = 1; i <= cnt; i++)
    {
        int pre = pi[i - 1], curt = pi[i];
        ll acc = 0;
        if (opt[curt])
            acc += process(curt - 1);
        acc += process(pre + 1), acc += process(curt);
    }
    printf("%lld\n", 1LL * ans.size());
    for (auto &x : ans)
        printf("%d\n", x);
    puts("");
    return 0;
}
```
