Submission #4285602


Source Code Expand

Copy
import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Arrays;
import java.io.IOException;
import java.io.BufferedReader;
import java.io.Reader;
import java.io.InputStreamReader;
import java.io.InputStream;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 */
public class Main {
    public static void main(String[] args) {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        MyInput in = new MyInput(inputStream);
        PrintWriter out = new PrintWriter(outputStream);
        DMatchMatching solver = new DMatchMatching();
        solver.solve(1, in, out);
        out.close();
    }

    static class DMatchMatching {
        int[] cnt = new int[10];
        int[] memo = new int[10000 + 1];

        public void solve(int testNumber, MyInput in, PrintWriter out) {
            int n = in.nextInt();
            int m = in.nextInt();
            Arrays.fill(cnt, 1 << 29);
            int[] use = new int[]{1 << 29, 2, 5, 5, 4, 5, 6, 3, 7, 6,};
            for (int x : in.nextIntArray(m)) {
                cnt[x] = use[x];
            }
            Arrays.fill(memo, -1);
            StringBuilder ans = new StringBuilder();
            while (n > 0) {
                for (int i = 9; i > 0; i--) {
                    if (dfs(n - cnt[i]) + 1 == dfs(n)) {
                        n -= cnt[i];
                        ans.append(i);
                        break;
                    }
                    if (i == 1) return;
                }
            }
            out.println(ans);
        }

        int dfs(int n) {
            if (n < 0) return -2;
            if (n == 0) return 0;
            if (memo[n] == -1) {
                for (int i = 1; i < 10; i++) {
                    memo[n] = Math.max(memo[n], dfs(n - cnt[i]) + 1);
                }
            }
            return memo[n];
        }

    }

    static class MyInput {
        private final BufferedReader in;
        private static int pos;
        private static int readLen;
        private static final char[] buffer = new char[1024 * 8];
        private static char[] str = new char[500 * 8 * 2];
        private static boolean[] isDigit = new boolean[256];
        private static boolean[] isSpace = new boolean[256];
        private static boolean[] isLineSep = new boolean[256];

        static {
            for (int i = 0; i < 10; i++) {
                isDigit['0' + i] = true;
            }
            isDigit['-'] = true;
            isSpace[' '] = isSpace['\r'] = isSpace['\n'] = isSpace['\t'] = true;
            isLineSep['\r'] = isLineSep['\n'] = true;
        }

        public MyInput(InputStream is) {
            in = new BufferedReader(new InputStreamReader(is));
        }

        public int read() {
            if (pos >= readLen) {
                pos = 0;
                try {
                    readLen = in.read(buffer);
                } catch (IOException e) {
                    throw new RuntimeException();
                }
                if (readLen <= 0) {
                    throw new MyInput.EndOfFileRuntimeException();
                }
            }
            return buffer[pos++];
        }

        public int nextInt() {
            int len = 0;
            str[len++] = nextChar();
            len = reads(len, isSpace);
            int i = 0;
            int ret = 0;
            if (str[0] == '-') {
                i = 1;
            }
            for (; i < len; i++) ret = ret * 10 + str[i] - '0';
            if (str[0] == '-') {
                ret = -ret;
            }
            return ret;
        }

        public char nextChar() {
            while (true) {
                final int c = read();
                if (!isSpace[c]) {
                    return (char) c;
                }
            }
        }

        int reads(int len, boolean[] accept) {
            try {
                while (true) {
                    final int c = read();
                    if (accept[c]) {
                        break;
                    }
                    if (str.length == len) {
                        char[] rep = new char[str.length * 3 / 2];
                        System.arraycopy(str, 0, rep, 0, str.length);
                        str = rep;
                    }
                    str[len++] = (char) c;
                }
            } catch (MyInput.EndOfFileRuntimeException e) {
            }
            return len;
        }

        public int[] nextIntArray(final int n) {
            final int[] res = new int[n];
            for (int i = 0; i < n; i++) {
                res[i] = nextInt();
            }
            return res;
        }

        static class EndOfFileRuntimeException extends RuntimeException {
        }

    }
}

Submission Info

Submission Time
Task D - Match Matching
User tanzaku
Language Java8 (OpenJDK 1.8.0)
Score 400
Code Size 5020 Byte
Status AC
Exec Time 94 ms
Memory 23380 KB

Judge Result

Set Name All Sample
Score / Max Score 400 / 400 0 / 0
Status
AC × 29
AC × 3
Set Name Test Cases
All 0_random_1, 0_random_2, 0_random_3, 0_random_4, 0_random_5, 0_random_6, 0_random_7, 0_random_8, 1_normal_1, 1_normal_2, 1_normal_3, 1_normal_4, 1_normal_5, 1_normal_6, 2_corner_1, 2_corner_2, 2_corner_3, 2_corner_4, 2_corner_5, 2_corner_6, 3_hand_1, 3_hand_2, 3_hand_3, 3_hand_4, 3_hand_5, 3_hand_6, sample_01, sample_02, sample_03
Sample sample_01, sample_02, sample_03
Case Name Status Exec Time Memory
0_random_1 AC 72 ms 19284 KB
0_random_2 AC 70 ms 21460 KB
0_random_3 AC 86 ms 20692 KB
0_random_4 AC 83 ms 23380 KB
0_random_5 AC 79 ms 20564 KB
0_random_6 AC 78 ms 21972 KB
0_random_7 AC 77 ms 20948 KB
0_random_8 AC 84 ms 20692 KB
1_normal_1 AC 87 ms 22484 KB
1_normal_2 AC 81 ms 22996 KB
1_normal_3 AC 85 ms 20948 KB
1_normal_4 AC 81 ms 23252 KB
1_normal_5 AC 82 ms 21460 KB
1_normal_6 AC 84 ms 21716 KB
2_corner_1 AC 80 ms 21076 KB
2_corner_2 AC 75 ms 21460 KB
2_corner_3 AC 92 ms 20308 KB
2_corner_4 AC 76 ms 19796 KB
2_corner_5 AC 77 ms 20820 KB
2_corner_6 AC 84 ms 22868 KB
3_hand_1 AC 87 ms 22996 KB
3_hand_2 AC 73 ms 19156 KB
3_hand_3 AC 71 ms 19280 KB
3_hand_4 AC 71 ms 21204 KB
3_hand_5 AC 71 ms 22356 KB
3_hand_6 AC 94 ms 20432 KB
sample_01 AC 71 ms 19924 KB
sample_02 AC 71 ms 20692 KB
sample_03 AC 72 ms 20564 KB