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

Solution in C++14 (GCC 5.4.1) (judge verdict: Wrong Answer)

#include <iostream>
#include <iomanip>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <algorithm>
#include <cstdio>
#include <utility>
#include <string>
#include <cmath>
#include <cstdlib>
#include <cstring>

using namespace std;

typedef uint64_t u64;
typedef int64_t s64;
typedef uint32_t u32;
typedef int32_t s32;
typedef vector<s32> vs32;
typedef vector<u32> vu32;
typedef vector<s64> vs64;
typedef vector<u64> vu64;

const double PI=3.14159265358979323846;

#define MAX(x, y) ((x) < (y) ? (y) : (x))
#define MIN(x, y) ((x) > (y) ? (y) : (x))

#define rep(i, N) for(int i = 0; i < N; ++i)

#define CEIL(x, y) (((x) + (y) - 1) / (y))
#define MOD 1000000007ULL

#define IN(l, r, x) ((l) <= (x) && (x) < (r))

int k;
int s[2000 + 1][2000 + 1];

void add(int x, int y, char c)
{
    int tx = x % k;
    int ty = y % k;
    int cx = x % (2 * k);
    int cy = y % (2 * k);

    char ini;
    if ((IN(0, k, cy) && IN(0, k, cx)) || (IN(k, 2 * k, cy) && IN(k, 2 * k, cx)))
    {
        ini = 'W';
    }
    else
    {
        ini = 'B';
    }

    if (ini == c)
    {
        ++s[ty + 1][tx + 1];
        ++s[ty + k + 1][tx + k + 1];
    }
    else
    {
        ++s[ty + k + 1][tx + 1];
        ++s[ty + 1][tx + k + 1];
    }
}

int main()
{
    cin.tie(0);
    ios::sync_with_stdio(false);

    int n;
    cin >> n >> k;

    int x, y;
    char c;
    rep (i, n)
    {
        cin >> x >> y >> c;
        add(x, y, c);
    }

    rep (i, 2 * k) rep (j, 2 * k) s[i + 1][j + 1] += s[i + 1][j] + s[i][j + 1] - s[i][j];

    int ans = 0;
    rep (i, k + 1)
    {
        rep (j, k + 1)
        {
            int w = s[i + k][j + k] - s[i + k][j] - s[i][j + k] + s[i][j];
            int b = k * k - w;
            ans = MAX(ans, MAX(w, b));
        }
    }

    cout << ans << "\n";
    return 0;
}