CSES - Datatähti 2017 alku - Results
Submission details
Task:Kolikot
Sender:planckcons
Submission time:2016-10-08 12:40:23 +0300
Language:Java
Status:COMPILE ERROR

Compiler report

input/Kolikot.java:21: error: cannot find symbol
            if(swch)
               ^
  symbol:   variable swch
  location: class Kolikot
input/Kolikot.java:21: error: illegal start of type
            if(swch)
              ^
input/Kolikot.java:24: error: cannot find symbol
                swch = false;
                ^
  symbol:   variable swch
  location: class Kolikot
input/Kolikot.java:30: error: cannot find symbol
                swch = true;
                ^
  symbol:   variable swch
  location: class Kolikot
4 errors

Code

import java.util.Scanner;

public class Kolikot {
    
    public static void main(String[] args) 
    {
        Scanner scan = new Scanner(System.in);
        int n = scan.nextInt();
        int[] arr = new int[n];
        
        for(int i = 0; i < n; i++)
        {
            arr[i] = scan.nextInt();
        }

//        int[] arr = {1, 2, 3, 9};
//        int n = arr.length;
        int k = 1;
        while(isSubsetSum1(arr, n, k))
        {
            if(swch)
            {
                k += n;
                swch = false;
            }
            else
            {
                k -= n;
                k++;
                swch = true;
            }
        }
        System.out.print(k);
    }
    
    static boolean isSubsetSum1(int arr[], int n, int sum) {

        boolean[][] subset = new boolean[sum + 1][n + 1];
        int i, j;

        for (i = 0; i <= n; i++) {
            subset[0][i] = true;
        }

        for (i = 1; i <= sum; i++) {
            subset[i][0] = false;
        }

        for (i = 1; i <= sum; i++) {
            for (j = 1; j <= n; j++) {
                subset[i][j] = subset[i][j - 1];

                if (i >= arr[j - 1]) {
                    subset[i][j] = subset[i][j] || subset[i - arr[j - 1]][j - 1];
                }
            }
        }
        return subset[sum][n];
    } 
}