I. Total currency exchange
public static void Main (string[] args) {
Scanner in = new Scanner (system.in);
int t = In.nextint ();
while (t--> 0) {
int N = In.nextint ();
int K = In.nextint ();
int[] coins = new Int[n];
for (int i = 0; i < N; i++) {
Coins[i] = In.nextint ();
}
Int[][] dp = new INT[N][K + 1];
for (int i = 0; i < N; i++) {
dp[i][0] = 1;
}
for (int i = 0; I <= K; i++) {
if (i% coins[0] = = 0) {
dp[0][i] = 1;
}
}
for (int i = 1; i < N; i++) {for
(int j = 0; J <= K; j) {
dp[i][j] = dp[i-1][j];
if (J >= Coins[i]) {
Dp[i][j] = = Dp[i][j-coins[i]]
;
DP[I][J]%= 100000007
}
}
System.out.println (Dp[n-1][k]);
}
second, the calf to send grass
private static int[] DP;
public static void Main (string[] args) {
Scanner in = new Scanner (system.in);
int t = In.nextint ();
while (t--> 0) {
int N = In.nextint ();
DP = new Int[n];
Int[] values = new Int[n];
for (int i = 0; i < N; i++) {
Values[i] = In.nextint ();
}
System.out.println (Math.max (Sell (values, 0, N-2), Sell (values, 1, N-1));
}
private static int sell (int[] values, int A, last) {
if (Last-first = 0) return Values[first];
if (Last-first = = 1) return Math.max (Values[first], values[last]);
Dp[first] = Values[first];
Dp[first + 1] = Values[first + 1];
Dp[first + 2] = Values[first] + Values[first + 2];
for (int i = i = 3; I <= last; i++) {
Dp[i] = Math.max (dp[i-2), dp[i-3]) + values[i];
Return Math.max (Dp[last], dp[last-1]);
}
Reference links