Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class onepunchman {
- static int r;
- static int[] x;
- static int[] a;
- public static void main(String[] args) {
- Scanner sc = new Scanner(System.in);
- int tc = sc.nextInt();
- sc.nextLine();
- for (int i = 0; i < tc; i++) {
- int n = sc.nextInt();
- r = sc.nextInt();
- int k = sc.nextInt();
- sc.nextLine();
- x = new int[n];
- a = new int[n];
- int total = 0;
- for (int j = 0; j < n; j++) {
- x[j] = sc.nextInt();
- total += sc.nextInt();
- a[j] = total;
- sc.nextLine();
- }
- states = new int[n][k + 1];
- for (int j = 0; j < n; j++) {
- for (int h = 0; h <= k; h++) {
- states[j][h] = -1;
- }
- }
- for (int j = n - 1; j >= 0; j--) {
- for (int q = 0; q <= k; q++) {
- dp(j, q);
- }
- }
- System.out.println(dp(0, k));
- }
- }
- static int[][] states;
- static int dp(int i, int k) {
- if (i == x.length) {
- return 0;
- } else {
- if (states[i][k] == -1) {
- int value;
- if (k > 0) {
- int low = i;
- int high = x.length - 1;
- int max = r * 2 + x[i];
- int j = i;
- while (high >= low) {
- int middle = (low + high) / 2;
- if (x[middle] <= max) {
- low = middle + 1;
- j = middle;
- }
- if (x[middle] > max) {
- high = middle - 1;
- }
- }
- int s;
- if (i > 0) {
- s = a[j] - a[i - 1];
- } else {
- s = a[j];
- }
- value = Math.max(dp(i + 1, k), dp(j + 1, k - 1) + s);
- } else {
- value = dp(i + 1, k);
- }
- states[i][k] = value;
- return value;
- } else {
- return states[i][k];
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement