AcWing 423. 采药
原题链接
简单
作者:
不知名的fE
,
2024-12-05 18:40:30
,
所有人可见
,
阅读 1
import java.util.*;
public class Main {
static final int N = 1010, M = 110;
static int[][] f = new int[N][N];
static int[][] a = new int[M][2];
static int n, m;
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String[] str = sc.nextLine().split(" ");
n = Integer.parseInt(str[0]); m = Integer.parseInt(str[1]);
for (int i = 1; i <= m; i++) {
str = sc.nextLine().split(" ");
int t = Integer.parseInt(str[0]), w = Integer.parseInt(str[1]);
a[i] = new int[] {t, w};
}
for (int i = 1; i <= m; i++)
for (int j = 1; j <= n; j++) {
f[i][j] = f[i - 1][j];
if (a[i][0] <= j) f[i][j] = Math.max(f[i][j], f[i - 1][j - a[i][0]] + a[i][1]);
}
System.out.println(f[m][n]);
}
}