#P2905. Rescue the Cows

    ID: 16163 Type: Default 1000ms 256MiB

Rescue the Cows

Rescue the Cows

John and his cows have formed a band called "Backstreet Cows" and are rehearsing in the pasture. There are $1000$ cow towers, each consisting of $30$ cows stacked one on top of the other. In the pasture, there are also $M$ haystacks where $1<M<1000$.

John, a superb conductor, can command the cow towers using his whistle that emits four distinct tones: $\mathtt{E}$, $\mathtt{W}$, $\mathtt{S}$, and $\mathtt{N}$, which correspond to moving one grid cell east, west, south, and north respectively.

At each whistle, all cow towers move simultaneously in the chosen direction. Whenever a cow tower lands on a cell containing a haystack, the top cow immediately jumps onto the haystack and remains there permanently, while the rest of the tower stays on that cell in its tower formation. Furthermore, if a tower is reduced to only one cow and lands on a haystack, that cow also jumps onto the haystack. Note that each tower can contribute at most $30$ cows.

John has $K$ whistles at his disposal. Initially, all towers are at position $(0,0)$. Determine the maximum number of cows that can be saved by planning an optimal sequence of $K$ moves, and output a corresponding sequence of moves. The sequence should be given as a string of exactly $K$ characters chosen from $\mathtt{E}$, $\mathtt{N}$, $\mathtt{S}$, and $\mathtt{W}$. In case there are multiple sequences yielding the same maximum number of saved cows, output the lexicographically smallest one.

inputFormat

The input begins with a line containing two integers $K$ and $M$, where:

  • $K$ is the number of whistle commands (moves).
  • $M$ is the number of haystacks.

Each of the following $M$ lines contains two integers $x$ and $y$, representing the coordinates of a haystack.

Note: All cow towers start at $(0,0)$.

outputFormat

Output two lines:

  1. The first line should contain a single integer representing the maximum number of cows that can be saved (keeping in mind that each tower can contribute at most $30$ cows, and there are $1000$ towers).
  2. The second line should contain a string of $K$ characters (each one being one of $\mathtt{E}$, $\mathtt{N}$, $\mathtt{S}$, or $\mathtt{W}$) denoting the lexicographically smallest sequence of moves that achieves this maximum.

sample

3 2
1 0
0 1
2000

ENW

</p>