Matrix Multiplication

Time limit2sMemory limit512 MB

Summary
For each prefix of n matrices, decide whether some multiplication order is valid, and if so report the largest possible area of the final product.
Level

Hard8 of 10

Topics
Greedy, Sorting, Implementation, Math
Solved
No attempts yet

Problem

You are given nn matrices M1,M2,…,MnM_1, M_2, \dots, M_n. Matrix MiM_i (i=1,2,…,ni = 1, 2, \dots, n) has R[i]R[i] rows and C[i]C[i] columns (R[i]×C[i]R[i] \times C[i]).

For an integer ii (1≤i≤n1 \le i \le n), you want to compute the value ViV_i defined as follows.

First, determine whether there is an order in which all ii matrices M1,M2,…,MiM_1, M_2, \dots, M_i can be multiplied. In matrix multiplication, to multiply an R×CR \times C matrix by an R′×C′R' \times C' matrix, we need C=R′C = R', and the resulting matrix is R×C′R \times C'. If the ii matrices cannot be multiplied even when their order is rearranged freely, define Vi=0V_i = 0. If they can, find the method that makes the final resulting matrix as large as possible, and the size of the resulting matrix (number of rows ×\times number of columns) is the value ViV_i.

Compute V1,V2,…,VnV_1, V_2, \dots, V_n.

Input

The first line contains the natural number nn (1≤n≤1,0001 \le n \le 1{,}000). The next nn lines each contain two integers, the number of rows R[i]R[i] and the number of columns C[i]C[i] of each matrix. The number of rows and columns of each matrix is between 1 and 1,000 inclusive.

Output

Output nn lines in total, and on each line output the value ViV_i from V1V_1 to VnV_n.

Examples5

  1. Example 1

    Input
    3
    8 2
    2 8
    2 2
    
    Expected output
    16
    64
    64
    
  2. Example 2

    Input
    3
    4 9
    9 1
    9 9
    
    Expected output
    36
    4
    4
    
  3. Example 3

    Input
    3
    4 10
    10 1
    10 6
    
    Expected output
    40
    4
    0
    
  4. Example 4

    Input
    3
    10 3
    3 10
    5 5
    
    Expected output
    30
    100
    0
    
  5. Example 5

    Input
    4
    1 1
    1 1
    1 1
    1 1
    
    Expected output
    1
    1
    1
    1