Submission #36163889
Source Code Expand
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const long long MOD = 998244353ll;
const long long INF = 2305843009213693951ll;
int main(void){
int N,X;
cin >> N >> X;
vector<ll> A(N);
for(int i=0;i<N;i++){
cin >> A[i];
}
//dp[i][j] = i番目まで行って所持金がjであるときの買える商品の最大値
int dp[101][10000];
for(int i=0;i<101;i++){
for(int j=0;j<10000;j++){
dp[i][j] = INT_MIN;
}
}
//Xとその並び替えは最初から使える
int x = X;
vector<int> Y;
while(x>0){
Y.push_back(x%10);
x/=10;
}
sort(Y.begin(),Y.end());
do{
int y = 0;
for(int j=0;j<(int)Y.size();j++){
y *= 10;
y += Y[j];
}
dp[0][y] = 0;
}while(next_permutation(Y.begin(),Y.end()));
//最も大きくなるように並べ替えたときのもの
ll S[10000];
for(int i=0;i<10000;i++){
vector<int> R;
int k = i;
while(k>0){
R.push_back(k%10);
k /= 10;
}
sort(R.begin(),R.end(),greater<>());
int r = 0;
for(int j=0;j<(int)R.size();j++){
r *= 10;
r += R[j];
}
S[i] = r;
}
//遷移
for(int i=0;i<N;i++){
for(int j=0;j<10000;j++){
//買う
if(j>=A[i]){
dp[i+1][j-A[i]] = max(dp[i+1][j-A[i]],dp[i][j]+1);
}
//買わない
dp[i+1][j] = max(dp[i+1][j],dp[i][j]);
}
//魔法を使って遷移元を集めてくる
for(int j=0;j<10000;j++){
dp[i+1][S[j]] = max(dp[i+1][S[j]],dp[i+1][j]);
}
for(int j=0;j<10000;j++){
int k = S[j];
while(k!=0 && k<10000){
dp[i+1][j] = max(dp[i+1][j],dp[i+1][k]);
k *= 10;
}
}
}
int Ans = 0;
for(int j=0;j<10000;j++){
Ans = max(Ans,dp[N][j]);
}
cout << Ans << endl;
return 0;
}
Submission Info
| Submission Time | |
|---|---|
| Task | B - Magical Wallet |
| User | x0214sh7 |
| Language | C++ (GCC 9.2.1) |
| Score | 200 |
| Code Size | 2195 Byte |
| Status | AC |
| Exec Time | 17 ms |
| Memory | 7628 KiB |
Judge Result
| Set Name | Sample | All | ||||
|---|---|---|---|---|---|---|
| Score / Max Score | 0 / 0 | 200 / 200 | ||||
| Status |
|
|
| Set Name | Test Cases |
|---|---|
| Sample | sample-01.txt, sample-02.txt, sample-03.txt, sample-04.txt |
| All | 01-01.txt, 01-02.txt, 01-03.txt, 01-04.txt, 01-05.txt, 01-06.txt, 01-07.txt, 02-01.txt, 02-02.txt, 02-03.txt, 02-04.txt, 02-05.txt, hand-01.txt, hand-02.txt, hand-03.txt, sample-01.txt, sample-02.txt, sample-03.txt, sample-04.txt |
| Case Name | Status | Exec Time | Memory |
|---|---|---|---|
| 01-01.txt | AC | 14 ms | 7472 KiB |
| 01-02.txt | AC | 14 ms | 7600 KiB |
| 01-03.txt | AC | 15 ms | 7604 KiB |
| 01-04.txt | AC | 15 ms | 7500 KiB |
| 01-05.txt | AC | 14 ms | 7528 KiB |
| 01-06.txt | AC | 14 ms | 7604 KiB |
| 01-07.txt | AC | 14 ms | 7524 KiB |
| 02-01.txt | AC | 12 ms | 7608 KiB |
| 02-02.txt | AC | 17 ms | 7524 KiB |
| 02-03.txt | AC | 13 ms | 7472 KiB |
| 02-04.txt | AC | 11 ms | 7528 KiB |
| 02-05.txt | AC | 12 ms | 7588 KiB |
| hand-01.txt | AC | 9 ms | 7628 KiB |
| hand-02.txt | AC | 14 ms | 7608 KiB |
| hand-03.txt | AC | 13 ms | 7504 KiB |
| sample-01.txt | AC | 8 ms | 7600 KiB |
| sample-02.txt | AC | 9 ms | 7600 KiB |
| sample-03.txt | AC | 9 ms | 7440 KiB |
| sample-04.txt | AC | 9 ms | 7416 KiB |