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 Rust (1.15.1) (judge verdict: Wrong Answer)

#![allow(unused_imports)]
use std::io::*;
use std::fmt::*;
use std::str::*;
use std::cmp::*;
use std::collections::*;

pub trait InputValue {
    fn parse(s: &str) -> Self;
}

pub fn read<T: InputValue>() -> T {
    let mut buf = String::new();
    let _ = stdin().read_line(&mut buf);
    T::parse(&buf.trim())
}

pub fn readn<T: InputValue>(n: usize) -> Vec<T> {
    let mut vec = vec![];
    for _ in 0..n {
        vec.push(read());
    }
    vec
}

pub fn readnc<T: InputValue>() -> Vec<T> {
    let mut vec = vec![];
    let line: String = read();
    for token in line.split_whitespace() {
        vec.push(T::parse(token));
    }
    vec
}

macro_rules! parse_single_value {
    ($($t:ty),*) => {
        $(
            impl InputValue for $t {
                fn parse(s: &str) -> $t { s.parse().unwrap() }
            }
        )*
	}
}
parse_single_value!(i32, i64, f32, f64, usize, String);

macro_rules! parse_tuple {
	($($t:ident),*) => {
		impl<$($t),*> InputValue for ($($t),*) where $($t: InputValue),* {
			fn parse(s: &str) -> ($($t),*) {
				let mut tokens = s.split_whitespace();
				let t = ($($t::parse(tokens.next().unwrap())),*);
				t
			}
		}
	}
}
parse_tuple!(A, B);
parse_tuple!(A, B, C);

// ===

fn main() {
    let (n, k): (usize, usize) = read();

    let l = k*2;
    let mut white_tbl = vec![vec![0; l]; l];
    let mut black_tbl = vec![vec![0; l]; l];

    for _i in 0..n {
        let (x, y, c): (usize, usize, String) = read();
        if c.chars().nth(0).unwrap() == 'B' {
            black_tbl[x%l][y%l] += 1;
        } else {
            white_tbl[x%l][y%l] += 1;
        }
    }

    let mut white_tbl_sum = vec![vec![0; l+1]; l+1];
    let mut black_tbl_sum = vec![vec![0; l+1]; l+1];

    for i in 1..l+1 {
        for j in 1..l+1 {
            black_tbl_sum[i][j] = black_tbl[i-1][j-1] - black_tbl_sum[i-1][j-1] + black_tbl_sum[i-1][j] + black_tbl_sum[i][j-1];
            white_tbl_sum[i][j] = white_tbl[i-1][j-1] - white_tbl_sum[i-1][j-1] + white_tbl_sum[i-1][j] + white_tbl_sum[i][j-1];
        }
    }

    let mut best = 0;
    for i in 0..l {
        for j in 0..l {
            best = max(best, solve(i, j, &black_tbl_sum, &white_tbl_sum));
        }
    }
    
    println!("{}", best);
}

fn solve(i: usize, j: usize, black_tbl: &Vec<Vec<i32>>, white_tbl: &Vec<Vec<i32>>) -> i32 {
    let l = black_tbl.len()-1;
    let k = l / 2;

    square_sum(i, j, k, black_tbl) + square_sum((i+k)%l, (j+k)%l, k, black_tbl)
     + square_sum((i+k)%l, j, k, white_tbl) + square_sum(i, (j+k)%l, k, white_tbl)
}

fn square_sum(fi: usize, fj: usize, k: usize, tbl: &Vec<Vec<i32>>) -> i32 {
    let l = k*2;
    let ti = min(fi+k, l);
    let tj = min(fj+k, l);
    let mut ret = rect_sum(fi, fj, ti, tj, tbl);
    if fi+k > l || fj+k > l {
        let fi2 = if fi+k > l { 0 } else { fi };
        let fj2 = if fj+k > l { 0 } else { fj };
        let ti2 = if fi+k > l { fi+k-l } else { ti };
        let tj2 = if fj+k > l { fj+k-l } else { tj };
        ret += rect_sum(fi2, fj2, ti2, tj2, tbl);
    }
    ret
}

fn rect_sum(fi: usize, fj: usize, ti: usize, tj: usize, tbl: &Vec<Vec<i32>>) -> i32 {
    tbl[ti][tj] - tbl[fi][tj] - tbl[ti][fj] + tbl[fi][fj]
}