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# (Mono 4.6.2.0) (judge verdict: Wrong Answer)

using System;
using System.Collections.Generic;
using System.Linq;
using BitArray = System.Collections.BitArray;
using BigInteger = System.Numerics.BigInteger;
using TextReader = System.IO.TextReader;
using System.Text;

namespace AtCoderProject
{
    public class Program
    {
        public object Calc()
        {
            var N = consoleReader.Int;
            K = consoleReader.Int;
            var K2 = 2 * K;
            var white = new int[K2, K2];
            var black = new int[K2, K2];
            foreach (var cr in consoleReader.Repeat(N))
            {
                var x = cr.Int % K2;
                var y = cr.Int % K2;
                if (cr.String == "W")
                    white[x, y]++;
                else
                    black[x, y]++;
            }
            whiteSum = new int[K2, K2];
            whiteSum[0, 0] = white[0, 0];
            blackSum = new int[K2, K2];
            blackSum[0, 0] = black[0, 0];
            for (int i = 1; i < K2; i++)
            {
                whiteSum[i, 0] = whiteSum[i - 1, 0] + white[i, 0];
                blackSum[i, 0] = blackSum[i - 1, 0] + black[i, 0];
                whiteSum[0, i] = whiteSum[0, i - 1] + white[0, i];
                blackSum[0, i] = blackSum[0, i - 1] + black[0, i];
            }
            for (int i = 1; i < K2; i++)
                for (int j = 1; j < K2; j++)
                {
                    whiteSum[i, j] = whiteSum[i, j - 1] + whiteSum[i - 1, j] - whiteSum[i - 1, j - 1] + white[i, j];
                    blackSum[i, j] = blackSum[i, j - 1] + blackSum[i - 1, j] - blackSum[i - 1, j - 1] + black[i, j];
                }

            var max = 0;
            for (int i = 0; i <= K; i++)
                for (int j = 0; j <= K; j++)
                {
                    var whiteCount = Count(whiteSum, i, j)
                        + Count(whiteSum, i - K, j - K)
                        + Count(whiteSum, i - K, j + K)
                        + Count(whiteSum, i + K, j - K)
                        + Count(whiteSum, i + K, j + K);
                    var blackCount =
                          Count(blackSum, i + K, j)
                        + Count(blackSum, i, j + K)
                        + Count(blackSum, i - K, j)
                        + Count(blackSum, i, j - K);
                    max = Math.Max(max, whiteCount + blackCount);
                }

            return max;
        }
        int Count(int[,] sums, int startX, int startY)
        {
            var endXexclusive = Math.Min(2 * K, startX + K);
            var endYexclusive = Math.Min(2 * K, startY + K);
            startX = Math.Max(0, startX);
            startY = Math.Max(0, startY);

            if (startX >= 2 * K || startY >= 2 * K || endXexclusive <= 0 || endYexclusive <= 0)
                return 0;

            if (startX == 0 && startY == 0)
                return sums[endXexclusive - 1, endYexclusive - 1];
            if (startX == 0)
                return sums[endXexclusive - 1, endYexclusive - 1] - sums[endXexclusive - 1, startY - 1];
            if (startY == 0)
                return sums[endXexclusive - 1, endYexclusive - 1] - sums[startX - 1, endYexclusive - 1];
            return sums[endXexclusive - 1, endYexclusive - 1] - sums[endXexclusive - 1, startY - 1] - sums[startX - 1, endYexclusive - 1] + sums[startX - 1, startY - 1];
        }
        int K;
        int[,] whiteSum;
        int[,] blackSum;

        #region いつもの
#pragma warning disable
        private ConsoleReader consoleReader;
        public Program(ConsoleReader consoleReader) { this.consoleReader = consoleReader; }
        static void Main() => Console.WriteLine(new Program(new ConsoleReader(Console.In)).Calc()); static string AllLines<T>(IEnumerable<T> source) => string.Join("\n", source);
    }
    public class ConsoleReader
    {
        private string[] ReadLineSplit() => textReader.ReadLine().Split(Array.Empty<char>(), StringSplitOptions.RemoveEmptyEntries);
        private string[] line = Array.Empty<string>();
        private int linePosition;
        private TextReader textReader;
        public ConsoleReader(TextReader tr) { textReader = tr; }
        public int Int => int.Parse(String);
        public long Long => long.Parse(String);
        public double Double => double.Parse(String);
        public string String
        {
            get
            {
                if (linePosition >= line.Length)
                {
                    linePosition = 0;
                    line = ReadLineSplit();
                }
                return line[linePosition++];
            }
        }
        public class SplitLine
        {
            private string[] splited;
            public SplitLine(ConsoleReader cr) { splited = cr.ReadLineSplit(); cr.line = Array.Empty<string>(); }
            public int[] Int => String.Select(x => int.Parse(x)).ToArray();
            public int[] Int0 => String.Select(x => int.Parse(x) - 1).ToArray();
            public long[] Long => String.Select(x => long.Parse(x)).ToArray();
            public double[] Double => String.Select(x => double.Parse(x)).ToArray();
            public string[] String => splited;
        }
        public SplitLine Split => new SplitLine(this);
        public class RepeatReader : IEnumerable<ConsoleReader>
        {
            ConsoleReader cr; int count;
            public RepeatReader(ConsoleReader cr, int count) { this.cr = cr; this.count = count; }
            public IEnumerator<ConsoleReader> GetEnumerator() => Enumerable.Repeat(cr, count).GetEnumerator();
            System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() => GetEnumerator();

            public IEnumerable<string> String => this.Select(cr => cr.String);
            public IEnumerable<int> Int => this.Select(cr => cr.Int);
            public IEnumerable<int> Int0 => this.Select(cr => cr.Int - 1);
            public IEnumerable<long> Long => this.Select(cr => cr.Long);
            public IEnumerable<double> Double => this.Select(cr => cr.Double);
        }
        public RepeatReader Repeat(int count) => new RepeatReader(this, count);
    }
    #endregion
}