-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbanknotes.cpp
54 lines (45 loc) · 975 Bytes
/
banknotes.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
void solve() {
int n, k;
cin >> n >> k;
vector<int> a(n);
for (int i = 0; i < n; i++) {
cin >> a[i];
}
// calc cost of each place
vector<ll> c(20);
int r_mx = 0;
int pt = 0;
ll cur_k = 0;
for (int i = 0; i < 20; i++) {
while (true) {
if (pt + 1 >= n) break;
if (a[pt + 1] > i) {
break;
}
pt++;
}
r_mx = max(r_mx, a[pt]);
c[i] = (ll)pow(10,i - r_mx);
cur_k += 9 * c[i];
}
ll ans = 0;
for (int p = 19; p >= 0; p--) {
for (int d = 9; d > 0; d--) {
cur_k -= c[p];
if (!(cur_k > k)) {
cur_k += c[p];
ans += (ll)pow(10, p) * d;
break;
}
}
}
cout << ans << endl;
}
int main() {
int t;
cin >> t;
while (t--) solve();
}