This page is still under construction.

Parts of this page are still being built. What you see may change.

The Traveler

Interview

Time limit1sMemory limit512 MB

Summary
Compute the height and width of the smallest axis-aligned rectangle containing a walk given in repeated direction blocks.
Level

Medium5 of 10

Topics
Simulation, Prefix sum, Math
Solved
No attempts yet

Problem

The traveler Bajtonson has just returned from another journey into the unknown. For obvious reasons he could not rely on a map, so he only recorded which way he was going. After every kilometer he wrote down the direction he had moved: N, S, E, or W (north, south, east, or west, respectively). In other words, each letter means one kilometer traveled in that direction.

Because there was little room in his notebook, he used shorthand. For example, 10 NSSW means Bajtonson repeated the sequence "1 km north, 2 km south, 1 km west" 10 times. Wherever one leg of the route (written as a shorthand) ended, the next leg began.

Back home, he wanted to draw a map of his travels, deciding that every kilometer of the journey would be one centimeter on the map. He now needs to buy a suitable sheet of paper but cannot judge how large it must be. So he has asked you, his assistant, to write a program that reads the travel notes and finds the dimensions of the smallest sheet (by area) that his route will fit on.

Naturally, like any map, Bajtonson's map must be a rectangle whose sides are parallel to the north-south and east-west axes.

Input

The first line of standard input contains one integer nn (1≤n≤10001 \le n \le 1000). Each of the next nn lines describes one leg of Bajtonson's journey. The ii-th line contains an integer kik_i (1≤ki≤200001 \le k_i \le 20000) followed by a space and a non-empty string of the letters N, S, E, and W. This means that during leg ii Bajtonson repeated the given direction sequence kik_i times. The total number of N, S, E, and W characters in the input does not exceed 10610^6.

Output

Print two integers separated by a single space: the height and the width, in centimeters, of the smallest sheet on which the traveler's route fits. You may assume that both numbers are no greater than 10910^9.

Hint

Examples3

  1. Example 1

    Input
    3
    3 NSSW
    1 ES
    10 E
    
    Expected output
    5 11
    
  2. Example 2

    Input
    1
    1 E
    
    Expected output
    0 1
    
  3. Example 3

    Input
    1
    3 NE
    
    Expected output
    3 3