Submission #69972629
Source Code Expand
Copy
#include <stdio.h>#include <stdlib.h>#include <string.h>int cmp(const void* x, const void* y) {char a = *(const char*)x, b = *(const char*)y;return a < b ? -1 : a > b;}/* {1,2,3} -> ... -> {3,2,1} */int next_permutation(char arr[], int n) {int target;int i;char temp;for (i = n - 2; i >= 0; i--) {if (arr[i] < arr[i + 1]) break;}if (i < 0) return 0;target = i;for (i = 0; target + i + 1 < n - i - 1; i++) {
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int cmp(const void* x, const void* y) {
char a = *(const char*)x, b = *(const char*)y;
return a < b ? -1 : a > b;
}
/* {1,2,3} -> ... -> {3,2,1} */
int next_permutation(char arr[], int n) {
int target;
int i;
char temp;
for (i = n - 2; i >= 0; i--) {
if (arr[i] < arr[i + 1]) break;
}
if (i < 0) return 0;
target = i;
for (i = 0; target + i + 1 < n - i - 1; i++) {
temp = arr[target + 1 + i];
arr[target + 1 + i] = arr[n - 1 - i];
arr[n - i - 1] = temp;
}
for (i = target + 1; i < n; i++) {
if (arr[i] > arr[target]) break;
}
temp = arr[i];
arr[i] = arr[target];
arr[target] = temp;
return 1;
}
int N, X;
int A[128];
int memo[128][11234];
int calc(int pos, int money) {
int ans = 0;
char buf[16];
int len;
if (pos >= N) return 0;
if (memo[pos][money]) return ~memo[pos][money];
snprintf(buf, sizeof(buf), "%d", money);
len = (int)strlen(buf);
qsort(buf, len, sizeof(*buf), cmp);
do {
int money2 = atoi(buf);
int candidate;
candidate = calc(pos + 1, money2);
if (candidate > ans) ans = candidate;
if (money2 >= A[pos]) {
candidate = calc(pos + 1, money2 - A[pos]) + 1;
if (candidate > ans) ans = candidate;
}
} while (next_permutation(buf, len));
return ~(memo[pos][money] = ~ans);
}
int main(void) {
int i;
if (scanf("%d%d", &N, &X) != 2) return 1;
for (i = 0; i < N; i++) {
if (scanf("%d", &A[i]) != 1) return 1;
}
printf("%d\n", calc(0, X));
return 0;
}
Submission Info
| Submission Time | |
|---|---|
| Task | B - Magical Wallet |
| User | mikecat |
| Language | C (gcc 12.2.0) |
| Score | 200 |
| Code Size | 1558 Byte |
| Status | AC |
| Exec Time | 427 ms |
| Memory | 5988 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 | 427 ms | 5988 KiB |
| 01-02.txt | AC | 5 ms | 2492 KiB |
| 01-03.txt | AC | 418 ms | 5968 KiB |
| 01-04.txt | AC | 408 ms | 5860 KiB |
| 01-05.txt | AC | 421 ms | 5988 KiB |
| 01-06.txt | AC | 420 ms | 5984 KiB |
| 01-07.txt | AC | 427 ms | 5904 KiB |
| 02-01.txt | AC | 222 ms | 3844 KiB |
| 02-02.txt | AC | 1 ms | 1968 KiB |
| 02-03.txt | AC | 383 ms | 5308 KiB |
| 02-04.txt | AC | 9 ms | 2016 KiB |
| 02-05.txt | AC | 82 ms | 2616 KiB |
| hand-01.txt | AC | 19 ms | 1888 KiB |
| hand-02.txt | AC | 0 ms | 1852 KiB |
| hand-03.txt | AC | 385 ms | 5456 KiB |
| sample-01.txt | AC | 0 ms | 1628 KiB |
| sample-02.txt | AC | 0 ms | 1640 KiB |
| sample-03.txt | AC | 0 ms | 1624 KiB |
| sample-04.txt | AC | 1 ms | 1812 KiB |