2010: company
Time Limit: 1 Sec Memory Limit: 256 MBSubmit: 81 Solved: 16
[ Submit][ Status][ Web Board]
Description
There are n kinds of goods in the company, with each of them has a inventory of cnti and direct unit benefit vali. Now you find due to price changes, for any goods sold on day i, if its direct benefit is val, the total benefit would be i⋅val.
Beginning from the first day, you can and must sell only one good per day until you can't or don't want to do so. If you are allowed to leave some goods unsold, what's the max total benefit you can get in the end?Input
The first line contains an integers n(1≤n≤1000).
The second line contains n integers val1,val2,..,valn(−100≤vali≤100).
The third line contains n integers cnt1,cnt2,..,cntn(1≤cnti≤100).
The second line contains n integers val1,val2,..,valn(−100≤vali≤100).
The third line contains n integers cnt1,cnt2,..,cntn(1≤cnti≤100).
Output
Output an integer in a single line, indicating the max total benefit.
Hint: sell goods whose price with order as -1, 5, 6, 6, the total benefit would be -1*1 + 5*2 + 6*3 + 6*4 = 51.
Hint: sell goods whose price with order as -1, 5, 6, 6, the total benefit would be -1*1 + 5*2 + 6*3 + 6*4 = 51.
Sample Input
4-1 -100 5 61 1 1 2
Sample Output
51
#include<cstdio>
#include<iostream>
#include<algorithm>
using namespace std;
typedef long long ll;
struct node
{
ll val;
ll cn;
}x[100005];
ll a[100005];
int main()
{
ll n;
while(~scanf("%lld",&n))
{
ll i,j,k=1;
for(i=1;i<=n;i++)
scanf("%lld",&x[i].val);
for(i=1;i<=n;i++)
{
scanf("%lld",&x[i].cn);
ll tt=x[i].cn;
while(tt--)
a[k++]=x[i].val;
}
sort(a+1,a+k);
ll mark=-1;
for(i=1;i<k;i++)
{
if(a[i]>=0)
{
mark=i;break;
}
}
if(mark==-1)
{
printf("0\n");continue;
}
ll temp=0,sum=0,_max;
for(i=mark;i<k;i++)
{
temp+=a[i]*(i-mark+1);
sum+=a[i];
}
_max=temp;
for(i=mark-1;i>=1;i--)
{
temp=temp+a[i]+sum;
sum+=a[i];
if(temp>=_max)
_max=temp;
}
printf("%lld\n",_max);
}
return 0;
}