---
id: 1642
lang: en
translation_of: index.md
source_sha256: 02955c843bd86d4941dc88f51b8cd67c7fe170d8b0943a7b233f5a208be0f4e9
title: "[Fortuna OJ] 4681 [GDOI2017 Mock] 8.11 Selection"
description: "Subtracting the minimum price from every item turns the problem into a DP for the fewest picks needed to reach each total."
tags: ["OI","DP","DP Optimization"]
translated: 2026-11-27
translator: Claude Opus 5.5
translator_model: claude-opus-5-5
translation_notes:
  - "选了i个次 (literally “picked i (items) times”, a slip) rendered as “made i picks”."
---

## Main Idea

In this problem, the upper bound on the value of any purchase plan is $a_i \times k$, roughly on the order of $1e5$. Consider a super brute force: let the state $f[i][j][w]$ be whether it is feasible to have made $i$ picks, with the last pick being coin type $j$ and the current total being $w$. Counting the value bound, this transition is roughly $O(n^4)$, which only passes part of the points.

Consider transforming the state. We first subtract the minimum price from the prices of all items; then the remaining purchase plans take the form $k*minVal + sum$. Then we let the state $f[i][j]$ be the minimum number of picks where the last pick is item type $i$ and the total reached is $j$, and we just count the states whose number of picks is less than $k$.

<!--more-->

## Code

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

using namespace std;

const int MAX_N = 550;

int n, k, dp[MAX_N * MAX_N], ai[MAX_N], mx_range, mi;

int main()
{
    scanf("%d%d", &n, &k);
    for (int i = 1; i <= n; i++)
        scanf("%d", &ai[i]);
    sort(ai + 1, ai + 1 + n);
    mi = ai[1];
    for (int i = 1; i <= n; i++)
        ai[i] -= mi;
    mx_range = ai[n];
    memset(dp, 0x3f, sizeof(dp));
    dp[0] = 0;
    for (int i = 1; i <= n; i++)
    {
        if (ai[i] == ai[i - 1])
            continue;
        for (int j = 1; j <= k * mx_range; j++)
            if (j >= ai[i])
                dp[j] = min(dp[j], dp[j - ai[i]] + 1);
    }
    for (int i = 0; i <= mx_range * k; i++)
        if (dp[i] <= k)
            printf("%d ", i + mi * k);
    return 0;
}
```
