CF BUDDY
← Problems·

1060C · Maximum Subrectangle

1600 · binary search, implementation, two pointers

Problem: You are given two arrays aa and bb of positive integers, with length nn and mm respectively.

Let cc be an n×mn \times m matrix, where ci,j=aibjc_{i,j} = a_i \cdot b_j.

You need to find a subrectangle of the matrix cc such that the sum of its elements is at most xx, and its area (the total number of elements) is the largest possible.

Formally, you need to find the largest number ss such that it is possible to choose integers x1,x2,y1,y2x_1, x_2, y_1, y_2 subject to 1x1x2n1 \leq x_1 \leq x_2 \leq n, 1y1y2m1 \leq y_1 \leq y_2 \leq m, (x2x1+1)×(y2y1+1)=s(x_2 - x_1 + 1) \times (y_2 - y_1 + 1) = s, and i=x1x2j=y1y2ci,jx.\sum_{i=x_1}^{x_2}{\sum_{j=y_1}^{y_2}{c_{i,j}}} \leq x.

Input Format: The first line contains two integers nn and mm (1n,m20001 \leq n, m \leq 2000).

The second line contains nn integers a1,a2,,ana_1, a_2, \ldots, a_n (1ai20001 \leq a_i \leq 2000).

The third line contains mm integers b1,b2,,bmb_1, b_2, \ldots, b_m (1bi20001 \leq b_i \leq 2000).

The fourth line contains a single integer xx (1x21091 \leq x \leq 2 \cdot 10^{9}).

Output Format: If it is possible to choose four integers x1,x2,y1,y2x_1, x_2, y_1, y_2 such that 1x1x2n1 \leq x_1 \leq x_2 \leq n, 1y1y2m1 \leq y_1 \leq y_2 \leq m, and i=x1x2j=y1y2ci,jx\sum_{i=x_1}^{x_2}{\sum_{j=y_1}^{y_2}{c_{i,j}}} \leq x, output the largest value of (x2x1+1)×(y2y1+1)(x_2 - x_1 + 1) \times (y_2 - y_1 + 1) among all such quadruplets, otherwise output 00.

Note: Matrix from the first sample and the chosen subrectangle (of blue color):

Matrix from the second sample and the chosen subrectangle (of blue color):

Sample Cases

Case 1

Input

3 3
1 2 3
1 2 3
9

Output

4

Case 2

Input

5 1
5 4 2 4 5
2
5

Output

1

Similar problems

00:00:00
Loading editor…
Welcome! I'm your coding tutor for this problem. Use the chips below to reveal stored hints or get AI feedback on your code. I'll guide you step by step — never giving away the solution.

Sign in to unlock AI tutor feedback