Problem:


Score : 500 points

Problem Statement

AtCoDeer is thinking of painting an infinite two-dimensional grid in a checked pattern of side K.
Here, a checked pattern of side K is a pattern where each square is painted black or white so that each connected component of each color is a K × K square.
Below is an example of a checked pattern of side 3:

AtCoDeer has N desires.
The i-th desire is represented by x_i, y_i and c_i.
If c_i is B, it means that he wants to paint the square (x_i,y_i) black; if c_i is W, he wants to paint the square (x_i,y_i) white.
At most how many desires can he satisfy at the same time?

Constraints

1 ≤ N ≤ 10^5

1 ≤ K ≤ 1000

0 ≤ x_i ≤ 10^9

0 ≤ y_i ≤ 10^9

If i ≠ j, then (x_i,y_i) ≠ (x_j,y_j).

c_i is B or W.

N, K, x_i and y_i are integers.

Input

Input is given from Standard Input in the following format:

N K
x_1 y_1 c_1
x_2 y_2 c_2
:
x_N y_N c_N

Output

Print the maximum number of desires that can be satisfied at the same time.

Sample Input 1

4 3
0 1 W
1 2 W
5 3 B
5 4 B

Sample Output 1

4

He can satisfy all his desires by painting as shown in the example above.

Sample Input 2

2 1000
0 0 B
0 1 W

Sample Output 2

2

Sample Input 3

6 2
1 2 B
2 1 W
2 2 B
1 0 B
0 6 W
4 5 W

Sample Output 3

4



Sample input:
4 3
0 1 W
1 2 W
5 3 B
5 4 B


Sample output:
4

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
ans=[[0]*(3*k+10) for i in range(4*k+10)]
for i in range(n):
    x,y,c=input().split()
    x=int(x)
    y=int(y)
    a=x//k%2
    b=x%k
    d=y//k%2
    e=y%k
    x=b+a*k
    y=e+d*k
    if (a+d)%2==0:
        if c=="B":

            x=k-b
            y=k-e

        else:
            x=2*k-b
            y=k-e
    else:
        if c == "W":
            x = k - b
            y = k - e

        else:
            x = 2 * k - b
            y = k - e

    ans[x][y]+=1
    ans[x][y+k]-=1
    ans[x+k][y]-=1
    ans[x+k][y+k]+=1


for i in range(4*k+9):
    for j in range(3*k+9):
        ans[i][j+1]=ans[i][j]+ans[i][j+1]
for i in range(4*k+9):
    for j in range(3*k+9):
        ans[i+1][j]=ans[i][j]+ans[i+1][j]
anser=0

for x in range(k,2*k):
    for y in range(k,2*k):
        p=ans[x][y]+ans[x-k][y-k]+ans[x-k][y+k]+ans[x+k][y+k]+ans[x+k][y-k]+ans[x+2*k][y]
        anser=max(anser,p)
for x in range(2*k,3*k):
    for y in range(k,2*k):
        p = ans[x][y] + ans[x - k][y - k] + ans[x - k][y + k] + ans[x + k][y + k] + ans[x + k][y - k]
        anser = max(anser, p)
print(anser)

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())

xyc=[input().split() for _ in range(n)]
xyc=[[int(x),int(y),c] for x,y,c in xyc]

blackacm=[[0]*(k+1) for _ in range(k+1)]
whiteacm=[[0]*(k+1) for _ in range(k+1)]

for x,y,c in xyc:
    if c=='B':
        if ((x//k)+(y//k))%2:
            blackacm[x%k+1][y%k+1]+=1
        else:
            whiteacm[x%k+1][y%k+1]+=1
    else:
        if ((x//k)+(y//k))%2:
            whiteacm[x%k+1][y%k+1]+=1
        else:
            blackacm[x%k+1][y%k+1]+=1

for i in range(1,k+1):
    for j in range(2,k+1):
        blackacm[i][j]+=blackacm[i][j-1]
        whiteacm[i][j]+=whiteacm[i][j-1]

for i in range(1,k+1):
    for j in range(2,k+1):
        blackacm[j][i]+=blackacm[j-1][i]
        whiteacm[j][i]+=whiteacm[j-1][i]

ans=0
for i in range(k):
    for j in range(k):
        tmp=0
        tmp+=blackacm[i][j]
        tmp+=blackacm[k][k]-blackacm[i][k]-blackacm[k][j]+blackacm[i][j]
        tmp+=whiteacm[i][k]+whiteacm[k][j]-whiteacm[i][j]
        ans=max(ans,tmp)
        ans=max(ans,n-tmp)
print(ans)

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())

xyc=[input().split() for _ in range(n)]
xyc=[[int(x),int(y),c] for x,y,c in xyc]

blackacm=[[0]*(k+1) for _ in range(k+1)]
whiteacm=[[0]*(k+1) for _ in range(k+1)]

for x,y,c in xyc:
    if c=='B':
        if ((x//k)+(y//k))%2:
            blackacm[x%k+1][y%k+1]+=1
        else:
            whiteacm[x%k+1][y%k+1]+=1
    else:
        if ((x//k)+(y//k))%2:
            whiteacm[x%k+1][y%k+1]+=1
        else:
            blackacm[x%k+1][y%k+1]+=1

for i in range(1,k+1):
    for j in range(2,k+1):
        blackacm[i][j]+=blackacm[i][j-1]
        whiteacm[i][j]+=whiteacm[i][j-1]

for i in range(1,k+1):
    for j in range(2,k+1):
        blackacm[j][i]+=blackacm[j-1][i]
        whiteacm[j][i]+=whiteacm[j-1][i]

ans=0
for i in range(k):
    for j in range(k):
        tmp=0
        tmp+=blackacm[i][j]
        tmp+=blackacm[k][k]-blackacm[i][k]-blackacm[k][j]+blackacm[i][j]
        tmp+=whiteacm[i][k]+whiteacm[k][j]-whiteacm[i][j]
        ans=max(ans,tmp)

print(ans)

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
ans=[[0]*(3*k+10) for i in range(4*k+10)]
for i in range(n):
    x,y,c=input().split()
    x=int(x)
    y=int(y)
    if c=="W":
        x+=k

    a=x//k%2
    b=x%k
    d=y//k%2
    e=y%k
    x=b+a*k
    y=e+d*k
    if (a+d)%2==0:


            x=k-b
            y=k-e


    else:

            x = k - b
            y = k - e


    ans[x][y]+=1
    ans[x][y+k]-=1
    ans[x+k][y]-=1
    ans[x+k][y+k]+=1


for i in range(4*k+9):
    for j in range(3*k+9):
        ans[i][j+1]=ans[i][j]+ans[i][j+1]
for i in range(4*k+3):
    for j in range(3*k+3):
        ans[i+1][j]=ans[i][j]+ans[i+1][j]
anser=0

for x in range(k,2*k):
    for y in range(k,2*k):
        p=ans[x][y]+ans[x-k][y-k]+ans[x-k][y+k]+ans[x+k][y+k]+ans[x+k][y-k]
        anser=max(anser,p)
for x in range(2*k,3*k):
    for y in range(k,2*k):
        p = ans[x][y] + ans[x - k][y - k] + ans[x - k][y + k] + ans[x + k][y + k] + ans[x + k][y - k]
        anser = max(anser, p)
print(anser)

Submission status: Wrong Answer

Solution code:
n,k = map(int, input().split())

ok = [ [0]*k for _ in range(k) ]
ng = [ [0]*k for _ in range(k) ]

ok_cnt = 0
ng_cnt = 0
for _ in range(n):
    x,y,c = map(str, input().split())
    x = int(x)%(k*2)
    y = int(y)%(k*2)
    if (x < k and y < k and c=='W') or (x >= k and y >= k and c=='W') or (x < k and y>= k and c =='B') or (x >= k and y < k and c =='B'):
        ok[x%k][y%k] += 1
        ok_cnt += 1
    else:
        ng[x%k][y%k] += 1
        ng_cnt += 1

ok_csums = [ [0]*(k+1) for _ in range(k+1)]
for i in range(k):
    for j in range(k):
        ok_csums[i+1][j+1] = ok_csums[i+1][j] + ok_csums[i][j+1] - ok_csums[i][j] + ok[i][j]

ng_csums = [ [0]*(k+1) for _ in range(k+1)]
for i in range(k):
    for j in range(k):
        ng_csums[i+1][j+1] = ng_csums[i+1][j] + ng_csums[i][j+1] - ng_csums[i][j] + ng[i][j]

    
ans = max(ok_cnt, ng_cnt)

# l u r b
for i in range(0,k+1):
    for j in range(0,k+1):
        ok_sum = ok_csums[i][j]
        ok_sum += (ok_csums[k][k] - ok_csums[i][k]- ok_csums[k][j] + ok_csums[i][j])
        ng_sum = ng_csums[i][j]
        ng_sum += (ng_csums[k][k] - ng_csums[i][k]- ng_csums[k][j] + ng_csums[i][j])
        # ng_sum = 2*ng_cnt - ng_sum
        ans = max(ok_sum+ng_sum, ans)

for i in range(0,k+1):
    for j in range(0,k+1):
        ok_sum = ok_csums[i][k] - ok_csums[i][j]
        ok_sum += (ok_csums[k][j] - ok_csums[i][j])
        ng_sum = ng_csums[i][k] - ng_csums[i][j]
        ng_sum += (ng_csums[k][j] - ng_csums[i][j])
        # ng_sum -= 2*ng_cnt
        # ng_sum = 2*ng_cnt - ng_sum
        ans = max(ok_sum+ng_sum, ans)


# # l u
# for i in range(1,k+1):
#     for j in range(1,k+1):
#         ok_sum = ok_csums[i][j]
#         ng_sum = ng_cnt - ng_csums[i][j]
#         ans = max(ok_sum+ng_sum, ans)

# # r b
# for i in range(0,k):
#     for j in range(0,k):
#         ok_sum = ok_csums[k][k] - ok_csums[i][k]-ok_csums[k][j] + ok_csums[i][j]
#         ng_sum = ng_cnt - (ng_csums[k][k] - ng_csums[i][k]-ng_csums[k][j] + ng_csums[i][j])
#         ans = max(ok_sum+ng_sum, ans)

# # l b
# for i in range(0,k):
#     for j in range(1,k+1):
#         ok_sum = ok_csums[k][j] - ok_csums[i][j]
#         ng_sum = ng_cnt - (ng_csums[k][j] - ng_csums[i][j])
#         ans = max(ok_sum+ng_sum, ans)


# # r u
# for i in range(1,k+1):
#     for j in range(0,k):
#         ok_sum = ok_csums[i][k] - ok_csums[i][j]
#         ng_sum = ng_cnt - (ng_csums[i][k] - ng_csums[i][j])
#         ans = max(ok_sum+ng_sum, ans)


print(ans)

Submission status: Wrong Answer

Solution code:
n,k = map(int, input().split())

ok = [ [0]*k for _ in range(k) ]
ng = [ [0]*k for _ in range(k) ]

ok_cnt = 0
ng_cnt = 0
for _ in range(n):
    x,y,c = map(str, input().split())
    x = int(x)%(k*2)
    y = int(y)%(k*2)
    if (x < k and y < k and c=='W') or (x >= k and y >= k and c=='W') or (x < k and y>= k and c =='B') or (x >= k and y < k and c =='B'):
        ok[x%k][y%k] += 1
        ok_cnt += 1
    else:
        ng[x%k][y%k] += 1
        ng_cnt += 1

ok_csums = [ [0]*(k+1) for _ in range(k+1)]
for i in range(k):
    for j in range(k):
        ok_csums[i+1][j+1] = ok_csums[i+1][j] + ok_csums[i][j+1] - ok_csums[i][j] + ok[i][j]

ng_csums = [ [0]*(k+1) for _ in range(k+1)]
for i in range(k):
    for j in range(k):
        ng_csums[i+1][j+1] = ng_csums[i+1][j] + ng_csums[i][j+1] - ng_csums[i][j] + ng[i][j]

    
ans = max(ok_cnt, ng_cnt)

# l u r b
for i in range(0,k+1):
    for j in range(0,k+1):
        ok_sum = ok_csums[i][j]
        ok_sum += (ok_csums[k][k] - ok_csums[i][k]- ok_csums[k][j] + ok_csums[i][j])
        ng_sum = ng_csums[i][j]
        ng_sum += (ng_csums[k][k] - ng_csums[i][k]- ng_csums[k][j] + ng_csums[i][j])
        ng_sum = 2*ng_cnt - ng_sum
        ans = max(ok_sum+ng_sum, ans)

for i in range(0,k+1):
    for j in range(0,k+1):
        ok_sum = ok_csums[i][k] - ok_csums[i][j]
        ok_sum += (ok_csums[k][j] - ok_csums[i][j])
        ng_sum = ng_csums[i][k] - ng_csums[i][j]
        ng_sum += (ng_csums[k][j] - ng_csums[i][j])
        # ng_sum -= 2*ng_cnt
        ng_sum = 2*ng_cnt - ng_sum
        ans = max(ok_sum+ng_sum, ans)


# # l u
# for i in range(1,k+1):
#     for j in range(1,k+1):
#         ok_sum = ok_csums[i][j]
#         ng_sum = ng_cnt - ng_csums[i][j]
#         ans = max(ok_sum+ng_sum, ans)

# # r b
# for i in range(0,k):
#     for j in range(0,k):
#         ok_sum = ok_csums[k][k] - ok_csums[i][k]-ok_csums[k][j] + ok_csums[i][j]
#         ng_sum = ng_cnt - (ng_csums[k][k] - ng_csums[i][k]-ng_csums[k][j] + ng_csums[i][j])
#         ans = max(ok_sum+ng_sum, ans)

# # l b
# for i in range(0,k):
#     for j in range(1,k+1):
#         ok_sum = ok_csums[k][j] - ok_csums[i][j]
#         ng_sum = ng_cnt - (ng_csums[k][j] - ng_csums[i][j])
#         ans = max(ok_sum+ng_sum, ans)


# # r u
# for i in range(1,k+1):
#     for j in range(0,k):
#         ok_sum = ok_csums[i][k] - ok_csums[i][j]
#         ng_sum = ng_cnt - (ng_csums[i][k] - ng_csums[i][j])
#         ans = max(ok_sum+ng_sum, ans)


print(ans)

Submission status: Wrong Answer

Solution code:
n,k = map(int, input().split())

ok = [ [0]*k for _ in range(k) ]
ng = [ [0]*k for _ in range(k) ]

ok_cnt = 0
ng_cnt = 0
for _ in range(n):
    x,y,c = map(str, input().split())
    x = int(x)%(k*2)
    y = int(y)%(k*2)
    if (x < k and y < k and c=='W') or (x >= k and y >= k and c=='W') or (x < k and y>= k and c =='B') or (x >= k and y < k and c =='B'):
        ok[x%k][y%k] += 1
        ok_cnt += 1
    else:
        ng[x%k][y%k] += 1
        ng_cnt += 1

ok_csums = [ [0]*(k+1) for _ in range(k+1)]
for i in range(k):
    for j in range(k):
        ok_csums[i+1][j+1] = ok_csums[i+1][j] + ok_csums[i][j+1] - ok_csums[i][j] + ok[i][j]

ng_csums = [ [0]*(k+1) for _ in range(k+1)]
for i in range(k):
    for j in range(k):
        ng_csums[i+1][j+1] = ng_csums[i+1][j] + ng_csums[i][j+1] - ng_csums[i][j] + ng[i][j]

    
ans = max(ok_cnt, ng_cnt)

# l u r b
for i in range(0,k+1):
    for j in range(0,k+1):
        ok_sum = ok_csums[i][j]
        ok_sum += (ok_csums[k][k] - ok_csums[i][k]- ok_csums[k][j] + ok_csums[i][j])
        ng_sum = ng_csums[i][j]
        ng_sum += (ng_csums[k][k] - ng_csums[i][k]- ng_csums[k][j] + ng_csums[i][j])
        ng_sum -= 2*ng_cnt
        ans = max(ok_sum+ng_sum, ans)

for i in range(0,k+1):
    for j in range(0,k+1):
        ok_sum = ok_csums[i][k] - ok_csums[i][j]
        ok_sum += (ok_csums[k][j] - ok_csums[i][j])
        ng_sum = ng_csums[i][k] - ng_csums[i][j]
        ng_sum += (ng_csums[k][j] - ng_csums[i][j])
        ng_sum -= 2*ng_cnt
        ans = max(ok_sum+ng_sum, ans)


# # l u
# for i in range(1,k+1):
#     for j in range(1,k+1):
#         ok_sum = ok_csums[i][j]
#         ng_sum = ng_cnt - ng_csums[i][j]
#         ans = max(ok_sum+ng_sum, ans)

# # r b
# for i in range(0,k):
#     for j in range(0,k):
#         ok_sum = ok_csums[k][k] - ok_csums[i][k]-ok_csums[k][j] + ok_csums[i][j]
#         ng_sum = ng_cnt - (ng_csums[k][k] - ng_csums[i][k]-ng_csums[k][j] + ng_csums[i][j])
#         ans = max(ok_sum+ng_sum, ans)

# # l b
# for i in range(0,k):
#     for j in range(1,k+1):
#         ok_sum = ok_csums[k][j] - ok_csums[i][j]
#         ng_sum = ng_cnt - (ng_csums[k][j] - ng_csums[i][j])
#         ans = max(ok_sum+ng_sum, ans)


# # r u
# for i in range(1,k+1):
#     for j in range(0,k):
#         ok_sum = ok_csums[i][k] - ok_csums[i][j]
#         ng_sum = ng_cnt - (ng_csums[i][k] - ng_csums[i][j])
#         ans = max(ok_sum+ng_sum, ans)


print(ans)

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
ans=[[0]*(3*k+10) for i in range(4*k+10)]
for i in range(n):
    x,y,c=input().split()
    x=int(x)
    y=int(y)
    a=x//k
    b=x%k
    d=y//k
    e=y%k
    if (a+d)%2==0:
        if c=="B":
            x=b+k
            y=e+k
            ans[x][y]+=1
            ans[x][y+k]-=1
            ans[x][y+k]-=1
            ans[x+k][y+k]+=1
        else:
            x=b+2*k
            y=e+k
            ans[x][y] += 1
            ans[x][y + k] -= 1
            ans[x][y + k] -= 1
            ans[x + k][y + k] += 1
    else:
        if c=="W":
            x=b+k
            y=e+k
            ans[x][y]+=1
            ans[x][y+k]-=1
            ans[x][y+k]-=1
            ans[x+k][y+k]+=1
        else:

            x=b+2*k
            y=e+k
            ans[x][y] += 1
            ans[x][y + k] -= 1
            ans[x][y + k] -= 1
            ans[x + k][y + k] += 1
for i in range(4*k+9):
    for j in range(3*k+9):
        ans[i][j+1]=ans[i][j]+ans[i][j+1]
for i in range(4*k+3):
    for j in range(3*k+3):
        ans[i+1][j]=ans[i][j]+ans[i+1][j]
anser=0

for x in range(k,2*k):
    for y in range(k,2*k):
        p=ans[x][y]+ans[x-k][y-k]+ans[x-k][y+k]+ans[x+k][y+k]+ans[x+k][y-k]
        anser=max(anser,p)
for x in range(2*k,3*k):
    for y in range(k,2*k):
        p = ans[x][y] + ans[x - k][y - k] + ans[x - k][y + k] + ans[x + k][y + k] + ans[x + k][y - k]
        anser = max(anser, p)
print(anser)

Submission status: Wrong Answer

Solution code:
n,k = map(int, input().split())

ok = [ [0]*k for _ in range(k) ]
ng = [ [0]*k for _ in range(k) ]

ok_cnt = 0
ng_cnt = 0
for _ in range(n):
    x,y,c = map(str, input().split())
    x = int(x)%(k*2)
    y = int(y)%(k*2)
    if (x < k and y < k and c=='W') or (x >= k and y >= k and c=='W') or (x < k and y>= k and c =='B') or (x >= k and y < k and c =='B'):
        ok[x%k][y%k] += 1
        ok_cnt += 1
    else:
        ng[x%k][y%k] += 1
        ng_cnt += 1

ok_csums = [ [0]*(k+1) for _ in range(k+1)]
for i in range(k):
    for j in range(k):
        ok_csums[i+1][j+1] = ok_csums[i+1][j] + ok_csums[i][j+1] - ok_csums[i][j] + ok[i][j]

ng_csums = [ [0]*(k+1) for _ in range(k+1)]
for i in range(k):
    for j in range(k):
        ng_csums[i+1][j+1] = ng_csums[i+1][j] + ng_csums[i][j+1] - ng_csums[i][j] + ng[i][j]

    
ans = max(ok_cnt, ng_cnt)
# l u
for i in range(1,k+1):
    for j in range(1,k+1):
        ok_sum = ok_csums[i][j]
        ng_sum = ng_cnt - ng_csums[i][j]
        ans = max(ok_sum+ng_sum, ans)

# r b
for i in range(0,k):
    for j in range(0,k):
        ok_sum = ok_csums[k][k] - ok_csums[i][k]-ok_csums[k][j] + ok_csums[i][j]
        ng_sum = ng_cnt - (ng_csums[k][k] - ng_csums[i][k]-ng_csums[k][j] + ng_csums[i][j])
        ans = max(ok_sum+ng_sum, ans)

# l b
for i in range(0,k):
    for j in range(1,k+1):
        ok_sum = ok_csums[k][j] - ok_csums[i][j]
        ng_sum = ng_cnt - (ng_csums[k][j] - ng_csums[i][j])
        ans = max(ok_sum+ng_sum, ans)


# r u
for i in range(1,k+1):
    for j in range(0,k):
        ok_sum = ok_csums[i][k] - ok_csums[i][j]
        ng_sum = ng_cnt - (ng_csums[i][k] - ng_csums[i][j])
        ans = max(ok_sum+ng_sum, ans)


print(ans)

Submission status: Wrong Answer

Solution code:
n,k = map(int, input().split())

ok = [ [0]*k for _ in range(k) ]
ng = [ [0]*k for _ in range(k) ]

ok_cnt = 0
ng_cnt = 0
for _ in range(n):
    x,y,c = map(str, input().split())
    x = int(x)%(k*2)
    y = int(y)%(k*2)
    if (x < k and y < k and c=='W') or (x >= k and y >= k and c=='W') or (x < k and y>= k and c =='B') or (x >= k and y < k and c =='B'):
        ok[x%k][y%k] += 1
        ok_cnt += 1
    else:
        ng[x%k][y%k] += 1
        ng_cnt += 1

ok_csums = [ [0]*(k+1) for _ in range(k+1)]
for i in range(k):
    for j in range(k):
        ok_csums[i+1][j+1] = ok_csums[i+1][j] + ok_csums[i][j+1] - ok_csums[i][j] + ok[i][j]

ng_csums = [ [0]*(k+1) for _ in range(k+1)]
for i in range(k):
    for j in range(k):
        ng_csums[i+1][j+1] = ng_csums[i+1][j] + ng_csums[i][j+1] - ng_csums[i][j] + ng[i][j]

    
ans = max(ok_cnt, ng_cnt)
minans = 100000000
# l u
for i in range(1,k+1):
    for j in range(1,k+1):
        ok_sum = ok_csums[i][j]
        ng_sum = ng_cnt - ng_csums[i][j]
        ans = max(ok_sum+ng_sum, ans)
        minans = min(minans, ok_sum+ng_sum)

# r b
for i in range(0,k+1):
    for j in range(0,k+1):
        ok_sum = ok_csums[k][k] - ok_csums[i][k]-ok_csums[k][j] + ok_csums[i][j]
        ng_sum = ng_cnt - (ng_csums[k][k] - ng_csums[i][k]-ng_csums[k][j] + ng_csums[i][j])
        ans = max(ok_sum+ng_sum, ans)
        minans = min(minans, ok_sum+ng_sum)


# l b
for i in range(0,k):
    for j in range(1,k+1):
        ok_sum = ok_csums[k][j] - ok_csums[i][j]
        ng_sum = ng_cnt - (ng_csums[k][j] - ng_csums[i][j])
        ans = max(ok_sum+ng_sum, ans)
        minans = min(minans, ok_sum+ng_sum)


# r u
for i in range(1,k+1):
    for j in range(0,k):
        ok_sum = ok_csums[i][k] - ok_csums[i][j]
        ng_sum = ng_cnt - (ng_csums[i][k] - ng_csums[i][j])
        ans = max(ok_sum+ng_sum, ans)
        minans = min(minans, ok_sum+ng_sum)

ans = max(ans, n-minans)
print(ans)

Submission status: Wrong Answer

Solution code:
n,k = map(int, input().split())

ok = [ [0]*k for _ in range(k) ]
ng = [ [0]*k for _ in range(k) ]

ok_cnt = 0
ng_cnt = 0
for _ in range(n):
    x,y,c = map(str, input().split())
    x = int(x)%(k*2)
    y = int(y)%(k*2)
    if (x < k and y < k and c=='W') or (x >= k and y >= k and c=='W') or (x < k and y>= k and c =='B') or (x >= k and y < k and c =='B'):
        ok[x%k][y%k] += 1
        ok_cnt += 1
    else:
        ng[x%k][y%k] += 1
        ng_cnt += 1

ok_csums = [ [0]*(k+1) for _ in range(k+1)]
for i in range(k):
    for j in range(k):
        ok_csums[i+1][j+1] = ok_csums[i+1][j] + ok_csums[i][j+1] - ok_csums[i][j] + ok[i][j]

ng_csums = [ [0]*(k+1) for _ in range(k+1)]
for i in range(k):
    for j in range(k):
        ng_csums[i+1][j+1] = ng_csums[i+1][j] + ng_csums[i][j+1] - ng_csums[i][j] + ng[i][j]

    
ans = max(ok_cnt, ng_cnt)
minans = 10**9
# l u
for i in range(1,k+1):
    for j in range(1,k+1):
        ok_sum = ok_csums[i][j]
        ng_sum = ng_cnt - ng_csums[i][j]
        ans = max(ok_sum+ng_sum, ans)
        minans = min(minans, ok_sum+ng_sum)

# r b
for i in range(0,k+1):
    for j in range(0,k+1):
        ok_sum = ok_csums[k][k] - ok_csums[i][k]-ok_csums[k][j] + ok_csums[i][j]
        ng_sum = ng_cnt - (ng_csums[k][k] - ng_csums[i][k]-ng_csums[k][j] + ng_csums[i][j])
        ans = max(ok_sum+ng_sum, ans)
        minans = min(minans, ok_sum+ng_sum)



# l b
for i in range(0,k):
    for j in range(1,k+1):
        ok_sum = ok_csums[k][j] - ok_csums[i][j]
        ng_sum = ng_cnt - (ng_csums[k][j] - ng_csums[i][j])
        ans = max(ok_sum+ng_sum, ans)
        minans = min(minans, ok_sum+ng_sum)



# r u
for i in range(1,k+1):
    for j in range(0,k):
        ok_sum = ok_csums[i][k] - ok_csums[i][j]
        ng_sum = ng_cnt - (ng_csums[i][k] - ng_csums[i][j])
        ans = max(ok_sum+ng_sum, ans)
        minans = min(minans, ok_sum+ng_sum)


ans = max(ans, n-minans)
print(ans)

Submission status: Wrong Answer

Solution code:
n,k = map(int, input().split())

ok = [ [0]*k for _ in range(k) ]
ng = [ [0]*k for _ in range(k) ]

ok_cnt = 0
ng_cnt = 0
for _ in range(n):
    x,y,c = map(str, input().split())
    x = int(x)%(k*2)
    y = int(y)%(k*2)
    if (x < k and y < k and c=='W') or (x >= k and y >= k and c=='W') or (x < k and y>= k and c =='B') or (x >= k and y < k and c =='B'):
        ok[x%k][y%k] += 1
        ok_cnt += 1
    else:
        ng[x%k][y%k] += 1
        ng_cnt += 1

ok_csums = [ [0]*(k+1) for _ in range(k+1)]
for i in range(k):
    for j in range(k):
        ok_csums[i+1][j+1] = ok_csums[i+1][j] + ok_csums[i][j+1] - ok_csums[i][j] + ok[i][j]

ng_csums = [ [0]*(k+1) for _ in range(k+1)]
for i in range(k):
    for j in range(k):
        ng_csums[i+1][j+1] = ng_csums[i+1][j] + ng_csums[i][j+1] - ng_csums[i][j] + ng[i][j]

    
ans = max(ok_cnt, ng_cnt)
# l u
for i in range(1,k+1):
    for j in range(1,k+1):
        ok_sum = ok_csums[i][j]
        ng_sum = ng_cnt - ng_csums[i][j]
        ans = max(ok_sum+ng_sum, ans)

# r b
for i in range(0,k+1):
    for j in range(0,k+1):
        ok_sum = ok_csums[k][k] - ok_csums[i][k]-ok_csums[k][j] + ok_csums[i][j]
        ng_sum = ng_cnt - (ng_csums[k][k] - ng_csums[i][k]-ng_csums[k][j] + ng_csums[i][j])
        ans = max(ok_sum+ng_sum, ans)

# l b
for i in range(0,k):
    for j in range(1,k+1):
        ok_sum = ok_csums[k][j] - ok_csums[i][j]
        ng_sum = ng_cnt - (ng_csums[k][j] - ng_csums[i][j])
        ans = max(ok_sum+ng_sum, ans)

# r u
for i in range(1,k+1):
    for j in range(0,k):
        ok_sum = ok_csums[i][k] - ok_csums[i][j]
        ng_sum = ng_cnt - (ng_csums[i][k] - ng_csums[i][j])
        ans = max(ok_sum+ng_sum, ans)

print(ans)

Submission status: Wrong Answer

Solution code:
n,k = map(int, input().split())

ok = [ [0]*k for _ in range(k) ]
ng = [ [0]*k for _ in range(k) ]

ok_cnt = 0
ng_cnt = 0
for _ in range(n):
    x,y,c = map(str, input().split())
    x = int(x)%(k*2)
    y = int(y)%(k*2)
    if (x < k and y < k and c=='W') or (x >= k and y >= k and c=='W') or (x < k and y>= k and c =='B') or (x >= k and y < k and c =='B'):
        ok[x%k][y%k] += 1
        ok_cnt += 1
    else:
        ng[x%k][y%k] += 1
        ng_cnt += 1

ok_csums = [ [0]*(k+1) for _ in range(k+1)]
for i in range(k):
    for j in range(k):
        ok_csums[i+1][j+1] = ok_csums[i+1][j] + ok_csums[i][j+1] - ok_csums[i][j] + ok[i][j]

ng_csums = [ [0]*(k+1) for _ in range(k+1)]
for i in range(k):
    for j in range(k):
        ng_csums[i+1][j+1] = ng_csums[i+1][j] + ng_csums[i][j+1] - ng_csums[i][j] + ng[i][j]

    
ans = ok_cnt
# l u
for i in range(1,k+1):
    for j in range(1,k+1):
        ok_sum = ok_csums[i][j]
        ng_sum = ng_cnt - ng_csums[i][j]
        ans = max(ok_sum+ng_sum, ans)

# r b
for i in range(0,k+1):
    for j in range(0,k+1):
        ok_sum = ok_csums[k][k] - ok_csums[i][k]-ok_csums[k][j] + ok_csums[i][j]
        ng_sum = ng_cnt - (ng_csums[k][k] - ng_csums[i][k]-ng_csums[k][j] + ng_csums[i][j])
        ans = max(ok_sum+ng_sum, ans)

# l b
for i in range(0,k):
    for j in range(1,k+1):
        ok_sum = ok_csums[k][j] - ok_csums[i][j]
        ng_sum = ng_cnt - (ng_csums[k][j] - ng_csums[i][j])
        ans = max(ok_sum+ng_sum, ans)

# r u
for i in range(1,k+1):
    for j in range(0,k):
        ok_sum = ok_csums[i][k] - ok_csums[i][j]
        ng_sum = ng_cnt - (ng_csums[i][k] - ng_csums[i][j])
        ans = max(ok_sum+ng_sum, ans)

print(ans)

Submission status: Wrong Answer

Solution code:
n,k = map(int, input().split())

ok = [ [0]*k for _ in range(k) ]
ng = [ [0]*k for _ in range(k) ]

ok_cnt = 0
ng_cnt = 0
for _ in range(n):
    x,y,c = map(str, input().split())
    x = int(x)%(k*2)
    y = int(y)%(k*2)
    if (x < k and y < k and c=='W') or (x >= k and y >= k and c=='W') or (x < k and y>= k and c =='B') or (x >= k and y < k and c =='B'):
        ok[x%k][y%k] += 1
        ok_cnt += 1
    else:
        ng[x%k][y%k] += 1
        ng_cnt += 1

ok_csums = [ [0]*(k+1) for _ in range(k+1)]
for i in range(k):
    for j in range(k):
        ok_csums[i+1][j+1] = ok_csums[i+1][j] + ok_csums[i][j+1] - ok_csums[i][j] + ok[i][j]

ng_csums = [ [0]*(k+1) for _ in range(k+1)]
for i in range(k):
    for j in range(k):
        ng_csums[i+1][j+1] = ng_csums[i+1][j] + ng_csums[i][j+1] - ng_csums[i][j] + ng[i][j]

    
ans = ok_cnt
# l u
for i in range(1,k+1):
    for j in range(1,k+1):
        ok_sum = ok_csums[i][j]
        ng_sum = ng_cnt - ng_csums[i][j]
        ans = max(ok_sum+ng_sum, ans)

# r b
for i in range(0,k+1):
    for j in range(0,k+1):
        ok_sum = ok_csums[k][k] - ok_csums[i][k]-ok_csums[k][j] + ok_csums[i][j]
        ng_sum = ng_cnt - (ng_csums[k][k] - ng_csums[i][k]-ng_csums[k][j] + ng_csums[i][j])
        ans = max(ok_sum+ng_sum, ans)

# l b
for i in range(0,k+1):
    for j in range(1,k+1):
        ok_sum = ok_csums[k][j] - ok_csums[i][j]
        ng_sum = ng_cnt - (ng_csums[k][j] - ng_csums[i][j])
        ans = max(ok_sum+ng_sum, ans)

# r u
for i in range(1,k+1):
    for j in range(0,k+1):
        ok_sum = ok_csums[i][k] - ok_csums[i][j]
        ng_sum = ng_cnt - (ng_csums[i][k] - ng_csums[i][j])
        ans = max(ok_sum+ng_sum, ans)

print(ans)

Submission status: Wrong Answer

Solution code:
n,k = map(int, input().split())

ok = [ [0]*k for _ in range(k) ]
ng = [ [0]*k for _ in range(k) ]

ok_cnt = 0
ng_cnt = 0
for _ in range(n):
    x,y,c = map(str, input().split())
    x = int(x)%(k*2)
    y = int(y)%(k*2)
    if (x < k and y < k and c=='W') or (x >= k and y >= k and c=='W') or (x < k and y>= k and c =='B') or (x >= k and y < k and c =='B'):
        ok[x%k][y%k] += 1
        ok_cnt += 1
    else:
        ng[x%k][y%k] += 1
        ng_cnt += 1

ok_csums = [ [0]*(k+1) for _ in range(k+1)]
for i in range(k):
    for j in range(k):
        ok_csums[i+1][j+1] = ok_csums[i+1][j] + ok_csums[i][j+1] - ok_csums[i][j] + ok[i][j]

ng_csums = [ [0]*(k+1) for _ in range(k+1)]
for i in range(k):
    for j in range(k):
        ng_csums[i+1][j+1] = ng_csums[i+1][j] + ng_csums[i][j+1] - ng_csums[i][j] + ng[i][j]

    
ans = ok_cnt
# l u
for i in range(0,k+1):
    for j in range(0,k+1):
        ok_sum = ok_csums[i][j]
        ng_sum = ng_cnt - ng_csums[i][j]
        ans = max(ok_sum+ng_sum, ans)

# r b
for i in range(0,k+1):
    for j in range(0,k+1):
        ok_sum = ok_csums[k][k] - ok_csums[i][k]-ok_csums[k][j] + ok_csums[i][j]
        ng_sum = ng_cnt - (ng_csums[k][k] - ng_csums[i][k]-ng_csums[k][j] + ng_csums[i][j])
        ans = max(ok_sum+ng_sum, ans)

# l b
for i in range(0,k+1):
    for j in range(0,k+1):
        ok_sum = ok_csums[k][j] - ok_csums[i][j]
        ng_sum = ng_cnt - (ng_csums[k][j] - ng_csums[i][j])
        ans = max(ok_sum+ng_sum, ans)

# r u
for i in range(0,k+1):
    for j in range(0,k+1):
        ok_sum = ok_csums[i][k] - ok_csums[i][j]
        ng_sum = ng_cnt - (ng_csums[i][k] - ng_csums[i][j])
        ans = max(ok_sum+ng_sum, ans)

print(ans)

Submission status: Wrong Answer

Solution code:
n,k = map(int, input().split())

ok = [ [0]*k for _ in range(k) ]
ng = [ [0]*k for _ in range(k) ]

ok_cnt = 0
ng_cnt = 0
for _ in range(n):
    x,y,c = map(str, input().split())
    x = int(x)%(k*2)
    y = int(y)%(k*2)
    if (x < k and y < k and c=='W') or (x >= k and y >= k and c=='W') or (x < k and y>= k and c =='B') or (x >= k and y < k and c =='B'):
        ok[x%k][y%k] += 1
        ok_cnt += 1
    else:
        ng[x%k][y%k] += 1
        ng_cnt += 1

ok_csums = [ [0]*(k+1) for _ in range(k+1)]
for i in range(k):
    for j in range(k):
        ok_csums[i+1][j+1] = ok_csums[i+1][j] + ok_csums[i][j+1] - ok_csums[i][j] + ok[i][j]

ng_csums = [ [0]*(k+1) for _ in range(k+1)]
for i in range(k):
    for j in range(k):
        ng_csums[i+1][j+1] = ng_csums[i+1][j] + ng_csums[i][j+1] - ng_csums[i][j] + ng[i][j]

    
ans = 0
# l u
for i in range(0,k+1):
    for j in range(0,k+1):
        ok_sum = ok_csums[i][j]
        ng_sum = ng_cnt - ng_csums[i][j]
        ans = max(ok_sum+ng_sum, ans)

# r b
for i in range(0,k+1):
    for j in range(0,k+1):
        ok_sum = ok_cnt - (ok_csums[i][k]-ok_csums[k][j]+ok_csums[i][j])
        ng_sum = (ng_csums[i][k]-ng_csums[k][j]+ng_csums[i][j])
        ans = max(ok_sum+ng_sum, ans)

# l b
for i in range(0,k+1):
    for j in range(0,k+1):
        ok_sum = ok_csums[k][j] - ok_csums[i][j]
        ng_sum = ng_cnt - (ng_csums[k][j] - ng_csums[i][j])
        ans = max(ok_sum+ng_sum, ans)

# r u
for i in range(0,k+1):
    for j in range(0,k+1):
        ok_sum = ok_csums[i][k] - ok_csums[i][j]
        ng_sum = ng_cnt - (ng_csums[i][k] - ng_csums[i][j])
        ans = max(ok_sum+ng_sum, ans)

print(ans)

Submission status: Wrong Answer

Solution code:
n,k = map(int, input().split())

ok = [ [0]*k for _ in range(k) ]
ng = [ [0]*k for _ in range(k) ]

ok_cnt = 0
ng_cnt = 0
for _ in range(n):
    x,y,c = map(str, input().split())
    x = int(x)%(k*2)
    y = int(y)%(k*2)
    if (x < k and y < k and c=='W') or (x >= k and y >= k and c=='W') or (x < k and y>= k and c =='B') or (x >= k and y < k and c =='B'):
        ok[x%k][y%k] += 1
        ok_cnt += 1
    else:
        ng[x%k][y%k] += 1
        ng_cnt += 1

ok_csums = [ [0]*(k+1) for _ in range(k+1)]
for i in range(k):
    for j in range(k):
        ok_csums[i+1][j+1] = ok_csums[i+1][j] + ok_csums[i][j+1] - ok_csums[i][j] + ok[i][j]

ng_csums = [ [0]*(k+1) for _ in range(k+1)]
for i in range(k):
    for j in range(k):
        ng_csums[i+1][j+1] = ng_csums[i+1][j] + ng_csums[i][j+1] - ng_csums[i][j] + ng[i][j]

    
ans = 0
# l u
for i in range(1,k+1):
    for j in range(1,k+1):
        ok_sum = ok_csums[i][j]
        ng_sum = ng_cnt - ng_csums[i][j]
        ans = max(ok_sum+ng_sum, ans)

# r b
for i in range(1,k+1):
    for j in range(1,k+1):
        ok_sum = ok_cnt - (ok_csums[i][k]-ok_csums[k][j]+ok_csums[i][j])
        ng_sum = (ng_csums[i][k]-ng_csums[k][j]+ng_csums[i][j])
        ans = max(ok_sum+ng_sum, ans)

# l b
for i in range(0,k+1):
    for j in range(1,k+1):
        ok_sum = ok_csums[k][j] - ok_csums[i][j]
        ng_sum = ng_cnt - (ng_csums[k][j] - ng_csums[i][j])
        ans = max(ok_sum+ng_sum, ans)

# r u
for i in range(0,k+1):
    for j in range(0,k):
        ok_sum = ok_csums[i][k] - ok_csums[i][j]
        ng_sum = ng_cnt - (ng_csums[i][k] - ng_csums[i][j])
        ans = max(ok_sum+ng_sum, ans)

print(ans)