Robot

There is a robot in each grid of an N × M Grid. These robots can execute commands. Commands include: NORTH All robots move one grid north. SOUTH All robots move one grid south. WEST All robots move one grid west. EAST All robots move one grid east. If a robot stays outside of the Grid after executing the command, it will be destroyed immediately. Given the total number of each type of command, you task is to arrange an order of these commands, so that the maximized number of commands (i.e. the maximal sum of the number of commands executed by each robot) are executed by these N ×M robots. (Note: a destroyed robot can not execute commands anymore.) Input Input contains several cases. The first line of each case contains two positive integers N and M indicating the number of rows and columns in the grid (1 ≤ N, M ≤ 105). The second line contains four integers indicating the number of each type of command : NORTH, SOUTH, WEST and EAST respectively. Each of the four numbers will not exceed 105. The last case is followed by a line containing two zeros. Output For each case, output an integer indicating the maximized number of commands that these robots can execute. The answer will be fit in a 64-bit signed integer. Sample Input 22 1000 22 1111 00 Sample Output Case 1: 4 Case 2: 9