PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN

#PS0000217. Tổng hình chữ nhật lớn nhất theo diện tích (Rectangle Sum)

HỌC THEO LỘ TRÌNH

Bài tập thuộc lộ trình

1 lộ trình

Tổng hình chữ nhật lớn nhất theo diện tích (Rectangle Sum)

Rectangle Sum

Source: AtCoder

Version: Phuoc Hung OJ Extended

Problem Statement

A square tray has N×NN\times N cells with tastiness values Di,jD_{i,j}. For each worker with capacity PP, choose a nonempty rectangle of consecutive cells of area at most PP, using every cell in it. Find the maximum total tastiness. Queries are independent.

Input

The first line contains NN, followed by NN rows of NN integers Di,jD_{i,j}. The next line contains QQ, followed by QQ lines each containing one integer PiP_i.

Output

Print QQ lines, the maximum tastiness for each worker.

Subtasks

  • Subtask 1 (50 points): 1≤N≤51\le N\le 5; 1≤Di,j≤1001\le D_{i,j}\le100; 1≤Q,Pi≤N21\le Q,P_i\le N^2.
  • Subtask 2 (50 points): 1≤N≤501\le N\le 50; 1≤Di,j≤1001\le D_{i,j}\le100; 1≤Q,Pi≤N21\le Q,P_i\le N^2.

Example 1

Input

3
3 2 1
2 2 1
1 1 1
3
1
4
9

Output

3
9
14

Explanation

For capacities 1, 4 and 9, choose the largest cell, the upper-left 2-by-2 square and the full tray, respectively.

Example 2

Input

3
1 1 1
1 1 1
9 9 9
1
4

Output

27

Explanation

The last row sums to 27 using only three cells; the capacity need not be filled.