---
id: 401
lang: en
translation_of: index.md
source_sha256: 0065586d6a6a4c63e3f698e2b3a0ad933d923dcc936d719dc4f5b73f61c817d8
title: "CH5105: Cookies — Solution"
description: "Sort the children, then run a linear DP over cookies given to the first i children with two transitions, recording choices to rebuild each child's share."
tags: ["OI","DP","Linear DP"]
translated: 2026-11-27
translator: Claude Opus 5.5
translator_model: claude-opus-5-5
translation_notes:
  - "升序 kept as “ascending order” as written, although the code sorts in descending order."
  - "思路清奇 rendered as “refreshingly unconventional”; treated as ordinary idiom rather than slang, so no chip."
---

## Main Idea

Obviously, this is a dynamic programming problem. The equation for this linear DP is extremely hard to derive, because if you derive it the normal way, you always have to consider the states before a state in order to do the DP. However, there is a refreshingly unconventional way to approach this problem.

First comes the preprocessing: we need to sort the children's anger values in ascending order, and record the mapping between each child's index before and after sorting.

```cpp
bool compare(const ll &a, const ll &b)
{ 
    return g[a] > g[b]; 
}

scanf("%lld%lld", &n, &m);
for (int i = 1; i <= n; i++)
    scanf("%lld", &g[i]), c[i] = i;
sort(c + 1, c + n + 1, compare);
```

The state is easy to come up with: $dp[i][j]$ means the first $i$ people have been handed $j$ cookies. The DP process, though, is truly magical. Consider two branches

- The first is to take one cookie away from each of the first $i$ people and add them to the $i$-th child. This way the anger values before it actually stay relatively unchanged, so we can transition directly: $dp[i][j] = dp[i][j-i]$.
- Next, we consider splitting off the middle segment and handling the taking-away separately. We can enumerate an endpoint $k \in [0,i)$, then keep the interval $[1,k]$ fixed and take some cookies away from the interval $[k+1,i]$. The transition should then be: $dp[i][j] = min\{ dp[k][j-(i-k)]+k*\sum^{i}_{p=k+1}g[p]\}$. We can write it in prefix-sum form: $dp[i][j] = min\{ dp[k][j-(i-k)]+k*(sum[i]-sum[k])\}$.

And with that, the DP part is done. We still need to count the cookies; we can record some information during the transitions to take care of the post-processing.

## Code

```cpp
// CH5105.cpp
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#define ll long long
using namespace std;
const int MX_N = 35, MX_M = 5100;
const ll INF = 0x3f3f3f3f3f3f3f3f;
ll n, m, g[MX_N], dp[MX_N][MX_M], c[MX_N], presum[MX_N];
ll lastPersonNum[MX_N][MX_M], cookieSum[MX_N][MX_M], ans[MX_N];
bool compare(const ll &a, const ll &b) { return g[a] > g[b]; }
void process(int num, int bis)
{
    if (num == 0)
        return;
    process(lastPersonNum[num][bis], cookieSum[num][bis]);
    if (lastPersonNum[num][bis] == num)
        for (int i = 1; i <= num; i++)
            ans[c[i]]++;
    else
        for (int i = lastPersonNum[num][bis] + 1; i <= num; i++)
            ans[c[i]] = 1;
}
int main()
{
    scanf("%lld%lld", &n, &m);
    for (int i = 1; i <= n; i++)
        scanf("%lld", &g[i]), c[i] = i;
    sort(c + 1, c + n + 1, compare);
    for (int i = 1; i <= n; i++)
        presum[i] = presum[i - 1] + g[c[i]];
    memset(dp, 0x3f, sizeof(dp));
    dp[0][0] = 0;
    for (int i = 1; i <= n; i++)
        for (int bis = i; bis <= m; bis++)
        {
            dp[i][bis] = dp[i][bis - i];
            lastPersonNum[i][bis] = i;
            cookieSum[i][bis] = bis - i;
            for (int k = 0; k < i; k++)
            {
                ll val = dp[k][bis - (i - k)] + k * (presum[i] - presum[k]);
                if (dp[i][bis] > val)
                    dp[i][bis] = val, lastPersonNum[i][bis] = k, cookieSum[i][bis] = bis - (i - k);
            }
        }
    printf("%lld\n", dp[n][m]);
    process(n, m);
    for (int i = 1; i <= n; i++)
        printf("%lld ", ans[i]);
    return 0;
}
```
