---
id: 2471
lang: en
translation_of: index.md
source_sha256: b0aee01718e0c96ef3b59e6bf0f82b74d0d59491a371cf14954922312e94a238
title: "[Universal OJ] #311: [UNR #2] Illness from Overwork — Solution"
description: "Divide and conquer on the maximum: a DP over length and maximum value that fixes the rightmost position of the maximum to avoid double counting."
tags: ["OI","DP"]
translated: 2026-11-27
translator: Claude Opus 5.5
translator_model: claude-opus-5-5
translation_notes:
  - "The problem name 积劳成疾 (an idiom, literally falling ill from accumulated overwork) is rendered as Illness from Overwork."
---

## Main Idea

This way of handling it is really neat. According to yyb's blog, this technique is called “divide and conquer on the maximum”.

We can consider designing the state $dp[i][j]$ as the number of ways for length $i$ with maximum value not exceeding $j$. Then we can enumerate a $k$ as the position of $j$, and force this $k$ to be the rightmost $j$ in this segment. Written out concretely, the transition is:

$$
dp[i][j] = dp[i][j - 1] + \sum_{k = 1}^i dp[k - 1][j] \times dp[i - k][j - 1] \times w^c
$$

Note the $dp[i - k][j - 1]$ on the right side, which guarantees that this position is the rightmost $j$, so nothing gets counted twice. Here $c$ is the number of intervals in the current sequence that contain this number; draw a picture, and just compute the difference between the left edges of the rightmost and leftmost intervals.

<!--more-->

## Code

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

using namespace std;

const int MAX_N = 440, mod = 998244353;

int n, m, wi[MAX_N][MAX_N], dp[MAX_N][MAX_N];

int main()
{
    scanf("%d%d", &n, &m);
    for (int i = 1; i <= n; i++)
    {
        scanf("%d", &wi[1][i]), wi[0][i] = 1;
        for (int j = 2; j <= m; j++)
            wi[j][i] = 1LL * wi[j - 1][i] * wi[1][i] % mod;
    }
    for (int i = 0; i <= n; i++)
        dp[0][i] = 1;
    for (int i = 1; i <= n; i++)
        for (int j = 1; j <= n; j++)
        {
            dp[i][j] = dp[i][j - 1];
            for (int k = 1; k <= i; k++)
            {
                int tmp = 1LL * dp[k - 1][j] * dp[i - k][j - 1] % mod;
                int idx = max(0, min(k, i - m + 1) - max(1, k - m + 1) + 1);
                // int idx = min({m, max(0, i - m + 1), k, i - k + 1});
                tmp = 1LL * tmp * wi[idx][j] % mod;
                dp[i][j] = (0LL + dp[i][j] + tmp) % mod;
            }
        }
    printf("%d\n", dp[n][n]);
    return 0;
}
```
