Contest 427 Problem B

Revision en1, by quinamatics, 2017-08-01 07:28:28

Hi, what is wrong with this code. I know that test case 4: 70,3326631213 is wrong, but I can't find the optimal solution.

import java.util.Arrays; import java.util.Scanner;

public class D2417B {

public static void main(String[] args) {
    Scanner s = new Scanner(System.in);
    int k = s.nextInt();
    s.nextLine();
    String str = s.nextLine();
    int[] arr = new int[str.length()];
    char[] ch = str.toCharArray();
    int sum =0;
    for(int i = 0;i < ch.length; i++){
       arr[i] = Character.getNumericValue(ch[i]);
       sum += arr[i];
    }

    Arrays.sort(arr);
    int fin = 0;
    if(sum >= k)
       System.out.println(0);
    else{
       sum = k-sum;
       for(int i = 0; i < arr.length; i++){
         if(sum + arr[i] <= 9){
          fin += Math.min(sum,10-sum);
          sum =0;
          break;
         }
         else{
          sum += arr[i];
          sum -= 9;
          fin += Math.min(9-arr[i],arr[i]+1);
         }
       }
       System.out.println(fin);
    }
}

}

History

 
 
 
 
Revisions
 
 
  Rev. Lang. By When Δ Comment
en1 English quinamatics 2017-08-01 07:28:28 971 Initial revision (published)