# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
523072 | Longgggggggg | Knapsack (NOI18_knapsack) | Java | 0 ms | 0 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
import java.io.*;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.InputMismatchException;
public class KnapSackSNOI {
public static void main(String[] args) {
FastIO io = new FastIO();
int s = io.nextInt();
int n = io.nextInt();
ArrayList<Pair>[] items = new ArrayList[s + 1];
for (int i = 0; i <= s; i++) items[i] = new ArrayList<>();
for (int i = 0; i < n; i++) {
int v = io.nextInt();
int w = io.nextInt();
int k = io.nextInt();
items[w].add(new Pair(v, k));
}
for (int i = 0; i <= s; i++) items[i].sort(Comparator.comparingInt((a) -> -a.v));
int[][] dp = new int[s + 1][s + 1];
for (int i = 1; i <= s; i++) {
for (int j = 0; j <= s; j++) {
dp[i][j] = dp[i - 1][j];
int curSum = j;
int curVal = 0;
for (int l = 0; l < items[i].size() && curSum >= i; l++) {
curSum -= i;
curVal += items[i].get(l).v;
dp[i][j] = Math.max(dp[i - 1][curSum] + curVal, dp[i][j]);
}
}
}
io.println(dp[s][s]);
io.close();
}
private static class Pair {
int v, k;
public Pair(int v, int k) {
this.v = v;
this.k = k;
}
}
private static class FastIO extends PrintWriter {
private final InputStream stream;
private final byte[] buf = new byte[1 << 16];
private int curChar, numChars;
// standard input
public FastIO() {
this(System.in, System.out);
}
public FastIO(InputStream i, OutputStream o) {
super(o);
stream = i;
}
// file input
public FastIO(String i, String o) throws IOException {
super(new FileWriter(o));
stream = new FileInputStream(i);
}
// throws InputMismatchException() if previously detected end of file
private int nextByte() {
if (numChars == -1) throw new InputMismatchException();
if (curChar >= numChars) {
curChar = 0;
try {
numChars = stream.read(buf);
} catch (IOException e) {
throw new InputMismatchException();
}
if (numChars == -1) return -1; // end of file
}
return buf[curChar++];
}
// to read in entire lines, replace c <= ' '
// with a function that checks whether c is a line break
public String next() {
int c;
do {
c = nextByte();
} while (c <= ' ');
StringBuilder res = new StringBuilder();
do {
res.appendCodePoint(c);
c = nextByte();
} while (c > ' ');
return res.toString();
}
public int nextInt() { // nextLong() would be implemented similarly
int c;
do {
c = nextByte();
} while (c <= ' ');
int sgn = 1;
if (c == '-') {
sgn = -1;
c = nextByte();
}
int res = 0;
do {
if (c < '0' || c > '9')
throw new InputMismatchException();
res = 10 * res + c - '0';
c = nextByte();
} while (c > ' ');
return res * sgn;
}
public long nextLong() { // nextLong() would be implemented similarly
int c;
do {
c = nextByte();
} while (c <= ' ');
int sgn = 1;
if (c == '-') {
sgn = -1;
c = nextByte();
}
long res = 0;
do {
if (c < '0' || c > '9')
throw new InputMismatchException();
res = 10 * res + c - '0';
c = nextByte();
} while (c > ' ');
return res * sgn;
}
public double nextDouble() {
return Double.parseDouble(next());
}
}
}