Saturday, 11 February 2012

Codechef -> Practice -> Easy -> PayingUp



Solution :



import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;

/**
 *
 * @author XCoder
 */
class FindSubset_PayingUp {

    public static int readIntLine() throws IOException {
        return Integer.parseInt(br.readLine());
    }

    public static long readLongLine() throws IOException {
        return Long.parseLong(br.readLine());
    }
    private static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    private static PrintWriter pw = new PrintWriter(System.out);

    public static void main(String[] args) throws IOException {
        int test = readIntLine();
        while (test-- > 0) {
            String str[] = br.readLine().split(" ");
            int noofnotes = Integer.parseInt(str[0]);
            int moneydemanded = Integer.parseInt(str[1]);

            int notes[] = new int[noofnotes];
            for (int i = 0; i < noofnotes; i++) {
                notes[i] = readIntLine();
            }

            boolean result = false;
            for (int u = 1; u < 1 << noofnotes; u++) {
                int x = u;
                int sum = 0;
                for (int i = 0; i < noofnotes; i++) {
                    if ((x & 1) > 0) {
                        sum += notes[i];
                    }
                    x = x >> 1;
                }
                if (sum == moneydemanded) {
                    result = true;
                    break;
                }
            }
            if (result) {
                pw.println("Yes");
            } else {
                pw.println("No");
            }
            pw.flush();
        }
        pw.close();
    }
}

No comments:

Post a Comment