• 매 시간마다 2중 for문을 통해 충전소 쌍(pair)의 모든 경우의 수를 확인한다.
    • 두 명이기 때문에 가능
    • 모든 경우의 수를 확인하면서 그 중 가장 큰 값을 찾는다.
      • 충전 가능한 충전소에 대해서는 해당 충전소의 충전량을, 불가능하면 0 값으로 설정
      • 같은 충전소의 경우 최대 한 번만 적용하면 된다(나눌 필요도 없다).
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
package swea.p5644_무선_충전;

import java.io.*;
import java.util.*;

public class Solution {
	static int M, A;
	static int[][] move;
	
	// 정지, 상, 우, 하, 좌
	static int[] dx = {0, -1, 0, 1,  0};
	static int[] dy = {0,  0, 1, 0, -1};
	
	static BattteryCharger[] battteryChargers;
	static class BattteryCharger {
		int r, c, dist, point;
		BattteryCharger(int r, int c, int dist, int point) {
			this.r = r; this.c = c;
			this.dist = dist; this.point = point;
		}
	}

	static int solution() {
		int sum = 0;
		int ar = 1, ac = 1;
		int br = 10, bc = 10;
		for (int i = 0; i <= M; i++) {
			ar += dx[move[i][0]];
			ac += dy[move[i][0]];
			br += dx[move[i][1]];
			bc += dy[move[i][1]];

			sum += calMax(ar, ac, br, bc);
		}
		return sum;
	}

	static int getPoint(int r, int c, int idx) {
		BattteryCharger bc = battteryChargers[idx];
		return Math.abs(bc.r - r) + Math.abs(bc.c - c) <= bc.dist? bc.point : 0;
	}

	static int calMax(int ar, int ac, int br, int bc) {
		int max = 0;
		for (int i = 0; i < A; i++) {
			for (int j = 0; j < A; j++) {
				int sum = 0;
				int aPoint = getPoint(ar, ac, i);
				int bPoint = getPoint(br, bc, j);

				if (i == j) sum = Math.max(aPoint, bPoint);
				else sum = aPoint + bPoint;

				max = Math.max(max, sum);
			}
		}
		return max;
	}

	public static void main(String[] args) throws Exception {
		BufferedReader in = new BufferedReader(new InputStreamReader(System.in));

		int testcase = Integer.parseInt(in.readLine());
		for (int tc = 1; tc <= testcase; tc++) {
			StringTokenizer st = new StringTokenizer(in.readLine());
			M = Integer.parseInt(st.nextToken());
			A = Integer.parseInt(st.nextToken());

			move = new int[M+1][2];
			for (int j = 0; j < 2; j++) {
				st = new StringTokenizer(in.readLine());
				for (int i = 1; i <= M; i++)
					move[i][j] = Integer.parseInt(st.nextToken());
			}

			battteryChargers = new BattteryCharger[A];
			for (int i = 0; i < A; i++) {
				st = new StringTokenizer(in.readLine());
				int sc = Integer.parseInt(st.nextToken());
				int sr = Integer.parseInt(st.nextToken());
				int depth = Integer.parseInt(st.nextToken());
				int point = Integer.parseInt(st.nextToken());
				
				battteryChargers[i] = new BattteryCharger(sr, sc, depth, point);
			}
			System.out.println("#" + tc + " " + solution());
		}
	}
}