Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5986781
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T22:43:15+00:00 2026-05-22T22:43:15+00:00

UPDATE : I have found the problem that my DP solution didn’t handle bonus

  • 0

UPDATE: I have found the problem that my DP solution didn’t handle bonus correctly. I added one more dimension to the state array to represent the sum of the first 6 categories. However, the solution got timed out. It’s not badly timeout since each test case can be solved less than 1 sec on my machine.

The problem description is here: http://uva.onlinejudge.org/external/101/10149.html

I searched online and found that it should be solved by DP and bitmask. I implemented the code and passed all test cases I tested, but the Uva Judge returns wrong answer.

My idea is to have state[i][j] to be matching round i to category bitmasked by j. Please point out my mistakes or link some code that can solve this problem correctly. Here is my code:

public class P10149 {

    public static void main(String[] args) throws IOException {
        Scanner in = new Scanner(new FileInputStream("input.txt"));
        // Scanner in = new Scanner(System.in);
        while (in.hasNextLine()) {
            int[][] round = new int[13][5];
            for (int i = 0; i < 13; i++) {
                for (int j = 0; j < 5; j++) {
                    round[i][j] = in.nextInt();
                }
            }
            in.nextLine();
            int[][] point = new int[13][13];
            for (int i = 0; i < 13; i++) {
                for (int j = 0; j < 13; j++) {
                    point[i][j] = getPoint(round[i], j);
                }
            }

            int[][] state = new int[14][1 << 13];
            for (int i = 1; i <= 13; i++) {
                Arrays.fill(state[i], -1);
            }
            int[][] bonusSum = new int[14][1 << 13];
            int[][] choice = new int[14][1 << 13];
            for (int i = 1; i <= 13; i++) {
                for (int j = 0; j < (1 << 13); j++) {
                    int usedSlot = 0;
                    for (int b = 0; b < 13; b++) {
                        if (((1 << b) & j) != 0) {
                            usedSlot++;
                        }
                    }
                    if (usedSlot != i) {
                        continue;
                    }
                    for (int b = 0; b < 13; b++) {
                        if (((1 << b) & j) != 0) {
                            int j2 = (~(1 << b) & j);
                            int bonus;
                            if (b < 6) {
                                bonus = bonusSum[i - 1][j2] + point[i - 1][b];
                            } else {
                                bonus = bonusSum[i - 1][j2];
                            }
                            int newPoint;
                            if (bonus >= 63 && bonusSum[i - 1][j2] < 63) {
                                newPoint = 35 + state[i - 1][j2] + point[i - 1][b];
                            } else {
                                newPoint = state[i - 1][j2] + point[i - 1][b];
                            }
                            if (newPoint > state[i][j]) {
                                choice[i][j] = b;
                                state[i][j] = newPoint;
                                bonusSum[i][j] = bonus;
                            }
                        }
                    }
                }
            }

            int index = (1 << 13) - 1;
            int maxPoint = state[13][index];
            boolean bonus = (bonusSum[13][index] >= 63);
            int[] mapping = new int[13];
            for (int i = 13; i >= 1; i--) {
                mapping[choice[i][index]] = i;
                index = (~(1 << choice[i][index]) & index);
            }

            for (int i = 0; i < 13; i++) {
                System.out.print(point[mapping[i] - 1][i] + " ");
            }
            if (bonus) {
                System.out.print("35 ");
            } else {
                System.out.print("0 ");
            }
            System.out.println(maxPoint);
        }
    }

    static int getPoint(int[] round, int category) {
        if (category < 6) {
            int sum = 0;
            for (int i = 0; i < round.length; i++) {
                if (round[i] == category + 1) {
                    sum += category + 1;
                }
            }
            return sum;
        }
        int sum = 0;
        int[] count = new int[7];
        for (int i = 0; i < round.length; i++) {
            sum += round[i];
            count[round[i]]++;
        }
        if (category == 6) {
            return sum;
        } else if (category == 7) {
            for (int i = 1; i <= 6; i++) {
                if (count[i] >= 3) {
                    return sum;
                }
            }
        } else if (category == 8) {
            for (int i = 1; i <= 6; i++) {
                if (count[i] >= 4) {
                    return sum;
                }
            }
        } else if (category == 9) {
            for (int i = 1; i <= 6; i++) {
                if (count[i] >= 5) {
                    return 50;
                }
            }
        } else if (category == 10) {
            for (int i = 1; i <= 3; i++) {
                if (isStraight(count, i, 4)) {
                    return 25;
                }
            }
        } else if (category == 11) {
            for (int i = 1; i <= 2; i++) {
                if (isStraight(count, i, 5)) {
                    return 35;
                }
            }
        } else if (category == 12) {
            for (int i = 1; i <= 6; i++) {
                for (int j = 1; j <= 6; j++) {
                    if (i != j && count[i] == 3 && count[j] == 2) {
                        return 40;
                    }
                }
            }
        }
        return 0;
    }

    static boolean isStraight(int[] count, int start, int num) {
        for (int i = start; i < start + num; i++) {
            if (count[i] == 0) {
                return false;
            }
        }
        return true;
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T22:43:16+00:00Added an answer on May 22, 2026 at 10:43 pm

    Here is the working solution.

    import java.io.FileInputStream;
    import java.io.IOException;
    import java.util.Arrays;
    import java.util.Scanner;
    
    public class P10149 {
    
        static final int MAX_BONUS_SUM = 115;
    
        public static void main(String[] args) throws IOException {
            Scanner in = new Scanner(new FileInputStream("input.txt"));
            // Scanner in = new Scanner(System.in);
            long t1 = System.currentTimeMillis();
            while (in.hasNextLine()) {
                int[][] round = new int[13][5];
                for (int i = 0; i < 13; i++) {
                    for (int j = 0; j < 5; j++) {
                        round[i][j] = in.nextInt();
                    }
                }
                in.nextLine();
                int[][] point = new int[13][13];
                for (int i = 0; i < 13; i++) {
                    for (int j = 0; j < 13; j++) {
                        point[i][j] = getPoint(round[i], j);
                    }
                }
    
                int[][] state = new int[1 << 13][MAX_BONUS_SUM + 1];
                int[][] newState = new int[1 << 13][MAX_BONUS_SUM + 1];
                for (int j = 0; j < (1 << 13); j++) {
                    Arrays.fill(state[j], -1);
                    Arrays.fill(newState[j], -1);
                }
                state[0][0] = 0;
                int[][][] choice = new int[13][1 << 13][MAX_BONUS_SUM + 1];
                for (int i = 0; i < 13; i++) {
                    for (int j = 0; j < (1 << 13); j++) {
                        int usedSlot = 0;
                        for (int b = 0; b < 13; b++) {
                            if (((1 << b) & j) != 0) {
                                usedSlot++;
                            }
                        }
                        if (usedSlot != i + 1) {
                            continue;
                        }
                        for (int b = 0; b < 13; b++) {
                            if (((1 << b) & j) != 0) {
                                int j2 = (~(1 << b) & j);
                                for (int s = 0; s <= MAX_BONUS_SUM; s++) {
                                    int oldSum;
                                    if (b < 6) {
                                        if (s < point[i][b]) {
                                            s = point[i][b] - 1;
                                            continue;
                                        }
                                        oldSum = s - point[i][b];
                                    } else {
                                        oldSum = s;
                                    }
                                    if (state[j2][oldSum] < 0) {
                                        continue;
                                    }
                                    int newPoint;
                                    if (s >= 63 && oldSum < 63) {
                                        newPoint = 35 + state[j2][oldSum] + point[i][b];
                                    } else {
                                        newPoint = state[j2][oldSum] + point[i][b];
                                    }
                                    if (newPoint > newState[j][s]) {
                                        choice[i][j][s] = b;
                                        newState[j][s] = newPoint;
                                    }
                                }
                            }
                        }
                    }
    
                    for (int j = 0; j < (1 << 13); j++) {
                        for (int s = 0; s <= MAX_BONUS_SUM; s++) {
                            state[j][s] = newState[j][s];
                        }
                        Arrays.fill(newState[j], -1);
                    }
                }
    
                int index = (1 << 13) - 1;
                int maxPoint = -1;
                int sum = 0;
                for (int s = 0; s <= MAX_BONUS_SUM; s++) {
                    if (state[index][s] > maxPoint) {
                        maxPoint = state[index][s];
                        sum = s;
                    }
                }
    
                boolean bonus = (sum >= 63);
                int[] mapping = new int[13];
                for (int i = 12; i >= 0; i--) {
                    mapping[choice[i][index][sum]] = i;
                    int p = 0;
                    if (choice[i][index][sum] < 6) {
                        p = point[i][choice[i][index][sum]];
                    }
                    index = (~(1 << choice[i][index][sum]) & index);
                    sum -= p;
                }
    
                for (int i = 0; i < 13; i++) {
                    System.out.print(point[mapping[i]][i] + " ");
                }
                if (bonus) {
                    System.out.print("35 ");
                } else {
                    System.out.print("0 ");
                }
                System.out.println(maxPoint);
            }
            long t2 = System.currentTimeMillis();
            // System.out.println(t2 - t1);
        }
    
        static int getPoint(int[] round, int category) {
            if (category < 6) {
                int sum = 0;
                for (int i = 0; i < round.length; i++) {
                    if (round[i] == category + 1) {
                        sum += category + 1;
                    }
                }
                return sum;
            }
            int sum = 0;
            int[] count = new int[7];
            for (int i = 0; i < round.length; i++) {
                sum += round[i];
                count[round[i]]++;
            }
            if (category == 6) {
                return sum;
            } else if (category == 7) {
                for (int i = 1; i <= 6; i++) {
                    if (count[i] >= 3) {
                        return sum;
                    }
                }
            } else if (category == 8) {
                for (int i = 1; i <= 6; i++) {
                    if (count[i] >= 4) {
                        return sum;
                    }
                }
            } else if (category == 9) {
                for (int i = 1; i <= 6; i++) {
                    if (count[i] >= 5) {
                        return 50;
                    }
                }
            } else if (category == 10) {
                for (int i = 1; i <= 3; i++) {
                    if (isStraight(count, i, 4)) {
                        return 25;
                    }
                }
            } else if (category == 11) {
                for (int i = 1; i <= 2; i++) {
                    if (isStraight(count, i, 5)) {
                        return 35;
                    }
                }
            } else if (category == 12) {
                for (int i = 1; i <= 6; i++) {
                    if (count[i] >= 5) {
                        return 40;
                    }
                }
                for (int i = 1; i <= 6; i++) {
                    for (int j = 1; j <= 6; j++) {
                        if (i != j && count[i] == 3 && count[j] == 2) {
                            return 40;
                        }
                    }
                }
            }
            return 0;
        }
    
        static boolean isStraight(int[] count, int start, int num) {
            for (int i = start; i < start + num; i++) {
                if (count[i] == 0) {
                    return false;
                }
            }
            return true;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

The problem: I often have to update two or more repositories: One for the
UPDATE: I now have a solution I'm much happier with that, whilst not solving
We have found a problem with our deployment to a production server that runs
UPDATE I am still debugging this issue and still have not found a solution.
I have an UPDATE sql command that modifies a Date/Time field in a particular
I have some update or something that tries to run every night, and ends
I have an update query being run by a cron task that's timing out.
I'm using an Ajax update panel and have recently added ASP.NET tracing code to
I've been messing with this all day and have still not found a solution.
I have a weird problem with Magento cache. I have an extension that have

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.