company
Problem Description
There are n kinds of goods in the company, with each of them has a inventory of and direct unit benefit . 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≤.≤100).
The third line contains n integers cnt1,cnt2,..,cntn(1≤≤100).
Output
Output an integer in a single line, indicating the max total benefit.
Example Input
4 -1 -100 5 6 1 1 1 2
Example Output
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.
Author
贪心,倒序维护即可,当前物品选定之后,之前选定的物品就都要多加一遍,所以维护一个后缀和即可。
过程维护一个最大值。时间复杂度O(n);
#include <cstdio>
#include<algorithm>
#include<cstring>
#include<iostream>
using namespace std;
typedef long long ll;
const int maxn=100000+5;
struct ss
{
int cnt,val;
};
int re[maxn];
int sum[maxn];
int main()
{
int n;
while(cin>>n)
{
int flag=0;
ss num[1005];
memset(re,0,sizeof(re));
memset(sum,0,sizeof(sum));
for(int i=0; i<n; i++)
{
scanf("%d",&num[i].val);
}
for(int i=0; i<n; i++)
{
scanf("%d",&num[i].cnt);
}
for(int i=0; i<n; i++)
{
for(int j=0; j<num[i].cnt; j++)
{
re[flag++]=num[i].val;
}
}
sort(re,re+flag);
ll ans=0;
ll a=0;
ll o=0;
for(int i=flag-1; i>=0; i--)
{
a+=re[i];
o+=a;
ans=max(ans,o);
}
printf("%lld\n",ans);
}
return 0;
}
/***************************************************
User name: zilong
Result: Accepted
Take time: 0ms
Take Memory: 1000KB
Submit time: 2017-07-23 16:09:20
****************************************************/