C. Maximum Subrectangle
http://codeforces.com/contest/1060/problem/C
You are given two arrays a and b of positive integers, with length n and m respectively.
Let c be an n×m matrix, where ci,j=ai⋅bj
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,y2subject to 1≤x1≤x2≤n1≤y1≤y2≤m, (x2−x1+1)×(y2−y1+1)=s=, and∑i=x1x2∑j=y1y2ci,j≤x.
Input
The first line contains two integers nn and mm (1≤n,m≤2000).
The second line contains nn integers a1,a2,…,an (1≤ai≤2000).
The third line contains mm integers b1,b2,…,bm(1≤bi≤2000).
The fourth line contains a single integer x (1≤x≤2⋅109).
Output
If it is possible to choose four integers x1,x2,y1,y2such that 1≤x1≤x2≤n1≤y1≤y2≤m, and ∑x2i=x1∑y2j=y1ci,j≤x, output the largest value of (x2−x1+1)×(y2−y1+1) among all such quadruplets, otherwise output 0.
Examples
input
Copy
3 3 1 2 3 1 2 3 9
output
Copy
4
input
Copy
5 1 5 4 2 4 5 2 5
output
Copy
1
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):
#include <bits/stdc++.h>
#define ll long long
using namespace std;
const ll inf=1e18;
ll a[2005],b[2005],mr[2005],mc[2005];
int main()
{
int n,m;
ll x,p;
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++)
{
scanf("%lld",&p);
a[i]=a[i-1]+p;
mr[i]=inf;
}
for(int i=1;i<=m;i++)
{
scanf("%lld",&p);
b[i]=b[i-1]+p;
mc[i]=inf;
}
cin>>x;
for(int i=1;i<=n;i++)
{
for(int j=0;j<i;j++)
{
mr[i-j]=min(mr[i-j],a[i]-a[j]);
}
}
for(int i=1;i<=m;i++)
{
for(int j=0;j<i;j++)
{
mc[i-j]=min(mc[i-j],b[i]-b[j]);
}
}
int maxx=0;
for(int i=1;i<=n;i++)
{
for(int j=1;j<=m;j++)
{
if(mr[i]*mc[j]<=x)
{
maxx=max(maxx,i*j);
}
}
}
printf("%d\n",maxx);
return 0;
}