Buggy python submission (verdict: Time Limit Exceeded):
N, K = map(int, input().split())

score = [[0 for x in range(K+1)] for y in range(K+1)]
base_hope = 0

for i in range(N):
    x, y, c = input().split()
    x2, y2 = int(x) // K, int(y) // K
    x_coord, y_coord = int(x) % K + 1, int(y) % K + 1
    if ((x2 + y2) % 2 == 0 and c == "W") or ((x2 + y2) % 2 == 1 and c == "B"):
        score[y_coord][x_coord] += 1
        base_hope += 1
    else:
        score[y_coord][x_coord] -= 1

xy = [(x, y) for x in range(K) for y in range(K)]
for x, y in xy:
    score[y+1][x+1] += score[y+1][x] + score[y][x+1] - score[y][x]

max_hope = 0
for x, y in xy:
    hope = base_hope - score[K][x] - score[y][K] + 2 * score[y][x]
    true_hope = max(hope, N - hope)
    if true_hope > max_hope:
        max_hope = true_hope

print(max_hope)

Fixed submission:
N, K = map(int, input().split())

score = [[0] * (K+1) for y in range(K+1)]
base_hope = 0

for i in range(N):
    x, y, c = input().split()
    x2, y2 = int(x) // K, int(y) // K
    x_coord, y_coord = int(x) % K + 1, int(y) % K + 1
    if ((x2 + y2) % 2 == 0 and c == "W") or ((x2 + y2) % 2 == 1 and c == "B"):
        score[y_coord][x_coord] += 1
        base_hope += 1
    else:
        score[y_coord][x_coord] -= 1

xy = [(x, y) for x in range(K) for y in range(K)]
for x, y in xy:
    score[y+1][x+1] += score[y+1][x] + score[y][x+1] - score[y][x]

max_hope = 0
for x, y in xy:
    hope = base_hope - score[K][x] - score[y][K] + 2 * score[y][x]
    true_hope = hope if hope > N - hope else N - hope
    if true_hope > max_hope:
        max_hope = true_hope

print(max_hope)

Buggy python submission (verdict: Time Limit Exceeded):

N, K = map(int, input().split())

# スコア行列[K, K]
# 白チェッカー左下を(x, y)としたときの正解数-誤答数
score_matrix = [[0 for x in range(K+1)] for y in range(K+1)]
base_hope = 0

for i in range(N):
    x, y, c = input().split()
    x2, y2 = int(x) // K, int(y) // K
    x_coord, y_coord = int(x) % K, int(y) % K
    # 正解
    if ((x2 + y2) % 2 == 0 and c == "W") or ((x2 + y2) % 2 == 1 and c == "B"):
        score_matrix[y_coord+1][x_coord+1] += 1
        base_hope += 1
    # 誤答
    else:
        score_matrix[y_coord+1][x_coord+1] -= 1

# スコア行列の二次元累積和を求める O(K^2)
xy = [(x, y) for x in range(1, K+1) for y in range(1, K+1)]
for x, y in xy:
    score_matrix[y][x] += score_matrix[y][x-1] + score_matrix[y-1][x] - score_matrix[y-1][x-1]

# 希望行列を累積和に基づき計算していく O(K^2)
# 同時に最大値を計算
max_hope = 0
for x, y in xy:
    hope = base_hope - score_matrix[K][x-1] - score_matrix[y-1][K] + 2 * score_matrix[y-1][x-1]
    true_hope = max(hope, N - hope)
    if true_hope > max_hope:
        max_hope = true_hope

print(max_hope)


Fixed submission:
N, K = map(int, input().split())

score = [[0] * (K+1) for y in range(K+1)]
base_hope = 0

for i in range(N):
    x, y, c = input().split()
    x2, y2 = int(x) // K, int(y) // K
    x_coord, y_coord = int(x) % K + 1, int(y) % K + 1
    if ((x2 + y2) % 2 == 0 and c == "W") or ((x2 + y2) % 2 == 1 and c == "B"):
        score[y_coord][x_coord] += 1
        base_hope += 1
    else:
        score[y_coord][x_coord] -= 1

xy = [(x, y) for x in range(K) for y in range(K)]
for x, y in xy:
    score[y+1][x+1] += score[y+1][x] + score[y][x+1] - score[y][x]

max_hope = 0
for x, y in xy:
    hope = base_hope - score[K][x] - score[y][K] + 2 * score[y][x]
    true_hope = hope if hope > N - hope else N - hope
    if true_hope > max_hope:
        max_hope = true_hope

print(max_hope)

Buggy python submission (verdict: Time Limit Exceeded):

# 入力部
N, K = map(int, input().split())
data_list = []
for i in range(N):
    x, y, c = input().split()
    x, y = int(x), int(y)
    data_list.append([x, y, c])

# スコア行列[K, K]
# 白チェッカー左下を(x, y)としたときの正解数-誤答数
score_matrix = [[0 for x in range(K)] for y in range(K)]

# 座標をmod Kに変換してスコア行列[K, K]を導出 O(N)
# 同時に初期値であるhope[0][0]も計算
score_matrix = [[0 for x in range(K)] for y in range(K)]
# 白チェッカー左下が(0, 0)のときの希望の数
base_hope = 0
for data in data_list:
    x, y, c = data[0] // K, data[1] // K, data[2]
    x_coord, y_coord = data[0] % K, data[1] % K
    # 正解
    if ((x + y) % 2 == 0 and c == "W") or ((x + y) % 2 == 1 and c == "B"):
        score_matrix[y_coord][x_coord] += 1
        base_hope += 1
    # 誤答
    else:
        score_matrix[y_coord][x_coord] -= 1
# print(score_matrix)

# スコア行列の二次元累積和を求める O(K^2)
xy = [(x, y) for x in range(K) for y in range(K)]
for x, y in xy:
    accum = score_matrix[y][x]
    if x > 0:
        accum += score_matrix[y][x-1]
    if y > 0:
        accum += score_matrix[y-1][x]
    if x > 0 and y > 0:
        accum -= score_matrix[y-1][x-1]
    score_matrix[y][x] = accum
# print(score_matrix)

# 希望行列を累積和に基づき計算していく O(K^2)
# 同時に最大値を計算
max_hope = 0
for x, y in xy:
    diff = 0
    if x > 0:
        diff += score_matrix[K-1][x-1]
    if y > 0:
        diff += score_matrix[y-1][K-1]
    if x > 0 and y > 0:
        diff -= 2 * score_matrix[y-1][x-1]
    hope = base_hope - diff
    true_hope = max(hope, N - hope)
    if true_hope > max_hope:
        max_hope = true_hope

print(max_hope)

Fixed submission:
N, K = map(int, input().split())

score = [[0] * (K+1) for y in range(K+1)]
base_hope = 0

for i in range(N):
    x, y, c = input().split()
    x2, y2 = int(x) // K, int(y) // K
    x_coord, y_coord = int(x) % K + 1, int(y) % K + 1
    if ((x2 + y2) % 2 == 0 and c == "W") or ((x2 + y2) % 2 == 1 and c == "B"):
        score[y_coord][x_coord] += 1
        base_hope += 1
    else:
        score[y_coord][x_coord] -= 1

xy = [(x, y) for x in range(K) for y in range(K)]
for x, y in xy:
    score[y+1][x+1] += score[y+1][x] + score[y][x+1] - score[y][x]

max_hope = 0
for x, y in xy:
    hope = base_hope - score[K][x] - score[y][K] + 2 * score[y][x]
    true_hope = hope if hope > N - hope else N - hope
    if true_hope > max_hope:
        max_hope = true_hope

print(max_hope)

Buggy python submission (verdict: Time Limit Exceeded):

# 入力部
N, K = map(int, input().split())
data_list = []
for i in range(N):
    x, y, c = input().split()
    x, y = int(x), int(y)
    data_list.append([x, y, c])

# スコア行列[K, K]
# 白チェッカー左下を(x, y)としたときの正解数-誤答数
score_matrix = [[0 for x in range(K)] for y in range(K)]

# 座標をmod Kに変換してスコア行列[K, K]を導出 O(N)
# 同時に初期値であるhope[0][0]も計算
score_matrix = [[0 for x in range(K)] for y in range(K)]
# 白チェッカー左下が(0, 0)のときの希望の数
base_hope = 0
for data in data_list:
    x, y, c = data[0] // K, data[1] // K, data[2]
    x_coord, y_coord = data[0] % K, data[1] % K
    # 正解
    if ((x + y) % 2 == 0 and c == "W") or ((x + y) % 2 == 1 and c == "B"):
        score_matrix[y_coord][x_coord] += 1
        base_hope += 1
    # 誤答
    else:
        score_matrix[y_coord][x_coord] -= 1
# print(score_matrix)

# スコア行列の二次元累積和を求める O(K^2)
for y in range(K):
    for x in range(K):
        accum = score_matrix[y][x]
        if x > 0:
            accum += score_matrix[y][x-1]
        if y > 0:
            accum += score_matrix[y-1][x]
        if x > 0 and y > 0:
            accum -= score_matrix[y-1][x-1]
        score_matrix[y][x] = accum
# print(score_matrix)

# 希望行列を累積和に基づき計算していく O(K^2)
# 同時に最大値を計算
max_hope = 0
for y in range(K):
    for x in range(K):
        diff = 0
        if x > 0:
            diff += score_matrix[K-1][x-1]
        if y > 0:
            diff += score_matrix[y-1][K-1]
        if x > 0 and y > 0:
            diff -= 2 * score_matrix[y-1][x-1]
        hope = base_hope - diff
        true_hope = max(hope, N - hope)
        if true_hope > max_hope:
            max_hope = true_hope

print(max_hope)

Fixed submission:
N, K = map(int, input().split())

score = [[0] * (K+1) for y in range(K+1)]
base_hope = 0

for i in range(N):
    x, y, c = input().split()
    x2, y2 = int(x) // K, int(y) // K
    x_coord, y_coord = int(x) % K + 1, int(y) % K + 1
    if ((x2 + y2) % 2 == 0 and c == "W") or ((x2 + y2) % 2 == 1 and c == "B"):
        score[y_coord][x_coord] += 1
        base_hope += 1
    else:
        score[y_coord][x_coord] -= 1

xy = [(x, y) for x in range(K) for y in range(K)]
for x, y in xy:
    score[y+1][x+1] += score[y+1][x] + score[y][x+1] - score[y][x]

max_hope = 0
for x, y in xy:
    hope = base_hope - score[K][x] - score[y][K] + 2 * score[y][x]
    true_hope = hope if hope > N - hope else N - hope
    if true_hope > max_hope:
        max_hope = true_hope

print(max_hope)

Buggy python submission (verdict: Time Limit Exceeded):

def count_ans(w_start, K, info_list):
    ans = 0
    for x, y, c in info_list:
        ofs_x = w_start[0]
        ofs_y = w_start[1]
        x2 = (x - ofs_x) // K
        y2 = (y - ofs_y) // K
        if (x2 + y2) % 2 == 0:
            if c == "W":
                ans += 1
        else:
            if c == "B":
                ans += 1
    return ans


# 入力
N, K = map(int, input().split())
info_list = []
for i in range(N):
    tmp = input().split()
    tmp[0], tmp[1] = int(tmp[0]), int(tmp[1])
    info_list.append(tmp)

# 白チェッカーの左下位置リスト
w_start_list = [[x, y] for x in range(K) for y in range(K)]

current_max = 0
for w_start in w_start_list:
    # 合っている個数を数える
    ans = count_ans(w_start, K, info_list)
    ans = max(N - ans, ans)
    if current_max < ans:
        current_max = ans

print(current_max)


Fixed submission:
N, K = map(int, input().split())

score = [[0] * (K+1) for y in range(K+1)]
base_hope = 0

for i in range(N):
    x, y, c = input().split()
    x2, y2 = int(x) // K, int(y) // K
    x_coord, y_coord = int(x) % K + 1, int(y) % K + 1
    if ((x2 + y2) % 2 == 0 and c == "W") or ((x2 + y2) % 2 == 1 and c == "B"):
        score[y_coord][x_coord] += 1
        base_hope += 1
    else:
        score[y_coord][x_coord] -= 1

xy = [(x, y) for x in range(K) for y in range(K)]
for x, y in xy:
    score[y+1][x+1] += score[y+1][x] + score[y][x+1] - score[y][x]

max_hope = 0
for x, y in xy:
    hope = base_hope - score[K][x] - score[y][K] + 2 * score[y][x]
    true_hope = hope if hope > N - hope else N - hope
    if true_hope > max_hope:
        max_hope = true_hope

print(max_hope)