求助
查看原帖
求助
981981
little_whitezzz楼主2023/6/14 09:03

好像c++要开long long 但是java怎么办?用大整数?

import java.io.*;
class read{
    StreamTokenizer st = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
    public int nextInt() throws IOException{
        st.nextToken();
        return (int)st.nval;
    }
}
public class Main {
    static int t, m;
    static int N = 1000010;
    static int[] f = new int[N];
    static int[] T = new int[100001];
    static int[] W = new int[100001];
    public static void main(String[] args) throws Exception{
        read sc = new read();
        t = sc.nextInt();
        m = sc.nextInt();
        for(int i = 1;i <= m;i ++){
            T[i] = sc.nextInt();
            W[i] = sc.nextInt();
        }
        for(int i = 1;i <= m;i ++) {
            for (int j = T[i]; j <= t; j++) {
                f[j] = Math.max(f[j], f[j - T[i]] + W[i]); //完全背包的dp
            }
        }
        System.out.print(f[t]);
    }
}
2023/6/14 09:03
加载中...