---
# System prepended metadata

title: Nomor Kandang Unofficial Editorial

---

# Nomor Kandang Unofficial Editorial
This editorial is made by [Joshua James](https://codeforces.com/profile/joshjms).

---

The question wants to find a certain number such that this is the greatest number you can buy with $m$. Let the first digit be $F$ and the temporary second until end digit be $S$. Let's form a number $FSSSS...$ such that the digits are maximized. Here we can easily notice that $S$ is simply the cheapest digit while $F$ is the highest possible digit that supports the largest number of digits.

To begin, we can iterate over $F$. Let's fix $S$ as the cheapest digit. Notice that $S$ can be any digit $(0 \leq S \leq N)$.

Define $D$ as the number of digits in $FSSSS...$. Also define $C_i$ as the cost of the digit $i$.

When iterating over $F$, we can simply skip $0$, as there shouldn't be any leading zeros. Let the number of digits when $F = i$ as $D_i$. $D_i = (m - C_i) / C_S + 1$. Until that part we want to maximize $D$.

So far, this is our code.

```cpp=
for(int i = 0; i <= n; i++){
  cin >> c[i];
  mn = comp(mn, {c[i], i});
}
cin >> m;
if(m >= c[0]) d = 0, F = 0, pref = "0";
for(int i = 1; i <= n; i++){
  if(d <= 0 && m >= c[i]) d = 0, F = i, pref = char(i + '0');
  int tmp = (m - c[i]) / mn.fi;
  if(tmp > 0 && tmp >= d){
    d = tmp;
    F = i;
    pref = char(i + '0');
  }
}
```

Note that the function `comp` is as follows.

```cpp=
pair <int, int> comp (pair<int,int> a, pair<int,int> b){
  if(a.fi < b.fi) return a;
  if(b.fi < a.fi) return b;
  if(a.se > b.se) return a;
  if(b.se > a.se) return b;
}
```