> For the complete documentation index, see [llms.txt](https://mayanktyagi3111.gitbook.io/interview-prep/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://mayanktyagi3111.gitbook.io/interview-prep/dynamic-programming/form-largest-integer-with-digits-that-add-up-to-target.md).

# Form Largest Integer With Digits That Add up to Target

Given an array of integers `cost` and an integer `target`. Return the **maximum** integer you can paint under the following rules:

* The cost of painting a digit (i+1) is given by `cost[i]` (0 indexed).
* The total cost used must be equal to `target`.
* Integer does not have digits 0.

Since the answer may be too large, return it as string.

If there is no way to paint any integer given the condition, return "0".

**Example 1:**

```
Input: cost = [4,3,2,5,6,7,2,5,5], target = 9
Output: "7772"
Explanation:  The cost to paint the digit '7' is 2, and the digit '2' is 3. Then cost("7772") = 2*3+ 3*1 = 9. You could also paint "977", but "7772" is the largest number.
Digit    cost
  1  ->   4
  2  ->   3
  3  ->   2
  4  ->   5
  5  ->   6
  6  ->   7
  7  ->   2
  8  ->   5
  9  ->   5
```

**Example 2:**

```
Input: cost = [7,6,5,5,5,6,8,7,8], target = 12
Output: "85"
Explanation: The cost to paint the digit '8' is 7, and the digit '5' is 5. Then cost("85") = 7 + 5 = 12.
```

**Example 3:**

```
Input: cost = [2,4,6,2,4,6,4,4,4], target = 5
Output: "0"
Explanation: It's not possible to paint any integer with total cost equal to target.
```

**Example 4:**

```
Input: cost = [6,10,15,40,40,40,40,40,40], target = 47
Output: "32211"
```

**Constraints:**

* `cost.length == 9`
* `1 <= cost[i] <= 5000`
* `1 <= target <= 5000`

```java
class Solution {
    public String largestNumber(int[] cost, int target) {
        // dp[i] -> Max number of digits required to make up target
        int[] dp = new int[target + 1];
        for (int t = 1; t <= target; ++t) {
            dp[t] = Integer.MIN_VALUE;
            for (int i = 0; i < 9; ++i) {
                if (t >= cost[i])
                    dp[t] = Math.max(dp[t], 1 + dp[t - cost[i]]);
            }
        }
        if (dp[target] < 0)
            return "0";
        StringBuilder res = new StringBuilder();
        // Type of backtracking of our dp solution
        for (int i = 8; i >= 0; --i) {
            while (target >= cost[i] && dp[target] == dp[target - cost[i]] + 1) {
                res.append(1 + i);
                target -= cost[i];
            }
        }
        return res.toString();
    }
}
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://mayanktyagi3111.gitbook.io/interview-prep/dynamic-programming/form-largest-integer-with-digits-that-add-up-to-target.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
