// Subset sums by dynamic programming // CSE 2320 Notes 7 import java.util.Scanner; public class subsetSum { public static void main(String[] args) { // Get input sequence Scanner sc=new Scanner(System.in); int n=sc.nextInt(); // Size of input set int m=sc.nextInt(); // Target value int S[]=new int[n+1]; // Input set S[0]=0; // Sentinel zero for (int i=1;i<=n;i++) S[i]=sc.nextInt(); // Initialize table for DP int C[]=new int[m+1]; C[0]=0; // DP base case // For each potential sum, determine the smallest index such // that its input value is in a subset to achieve that sum. for (int potentialSum=1; potentialSum<=m; potentialSum ++) { int j; for (j=1;j<=n;j++) { int leftover=potentialSum-S[j]; // To be achieved with other values if (leftover<0) // Too much thrown away continue; if (C[leftover]==(-1)) // No way to achieve leftover continue; if (C[leftover]0;i-=S[C[i]]) System.out.format("%3d %3d\n",C[i],S[C[i]]); } } }