AcWing 426. 开心的金明
原题链接
简单
作者:
不知名的fE
,
2024-12-07 11:02:51
,
所有人可见
,
阅读 1
import java.util.*;
public class Main {
static final int N = 30010, M = 30;
static int[] f = new int[N], v = new int[M], w = new int[M];
static int n, m;
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
m = sc.nextInt();
n = sc.nextInt();
for (int i = 1; i <= n; i++) {
v[i] = sc.nextInt();
w[i] = v[i] * sc.nextInt();
}
for (int i = 1; i <= n; i++)
for (int j = m; j >= v[i]; j--)
f[j] = Math.max(f[j], f[j - v[i]] + w[i]);
System.out.println(f[m]);
}
}
import java.util.*;
public class Main {
static final int N = 30010, M = 30;
static int[] f = new int[N], v = new int[M], w = new int[M];
static int n, m;
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
m = sc.nextInt();
n = sc.nextInt();
for (int i = 1; i <= n; i++) {
int v = sc.nextInt(), w = v * sc.nextInt();
for (int j = m; j >= v; j--)
f[j] = Math.max(f[j], f[j - v] + w);
}
System.out.println(f[m]);
}
}