# pizza location

unknown
java
22 days ago
1.7 kB
3
Indexable
Never
```import java.util.Scanner;

public class Solution {
static int k, r;
static int m;
static int loca[][];
static int n;
static int pizza[][];
static int ans;
static boolean visit[];

static int dis(int x1, int y1, int x2, int y2) {
return (x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2);
}

static void backTrack(int idx, int count, int sum) {
if (count == k) {
if (ans < sum) {
ans = sum;
}
return;
}

if(idx == m + 1){
return;
}

backTrack(idx + 1, count + 1, sum + solve(loca[idx][0], loca[idx][1]));
visit = new boolean[n+1];
backTrack(idx + 1, count, sum);

}

static int solve(int x, int y) {
int sum = 0;
int d = 0;
for (int j = 1; j <= n; j++) {
if(!visit[j]){
d = dis(x, y, pizza[j][0], pizza[j][1]);
if (d <= r * r) {
visit[j] = true;
sum += pizza[j][2];
}
}

}
return sum;
}

public static void main(String[] args) throws Exception {
Scanner sc = new Scanner(System.in);
int T = sc.nextInt();
for (int tc = 1; tc <= T; tc++) {
k = sc.nextInt();
r = sc.nextInt();
m = sc.nextInt();
loca = new int[m + 1][2];
for (int i = 1; i <= m; i++) {
loca[i][0] = sc.nextInt();
loca[i][1] = sc.nextInt();
}

n = sc.nextInt();
pizza = new int[n + 1][3];
for (int i = 1; i <= n; i++) {
pizza[i][0] = sc.nextInt();
pizza[i][1] = sc.nextInt();
pizza[i][2] = sc.nextInt();
}

ans = 0;
visit = new boolean[n + 1];
backTrack(1, 0, 0);
System.out.println("#" + tc + " " + ans);

}
sc.close();
}
}
```