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
题目大意:
给你N种物品,每种物品已知两个元素,价值和个数。
每天可以卖出一个物品,第一天卖出的价值是val.第二天卖出的价值是val*2,第三天卖出的价值是val*3.............依次类推。
现在可以舍弃一些物品不去卖,问最大收益。
思路:
首先肯定是要按照物品价值从小到大排序的。
而且我们已知如果商品的价值>0就一定不要舍弃掉,所以我们不舍弃掉的价值<0的部分就是为了垫高天数的。
所以哦们按照物品价值从小到大排序之后呢,我们只要枚举一个物品作为第一天卖出的物品然后向后扫维护最大值即可。
那么时间复杂度O(n^2);
直接这么做肯定要TLE.
所以我们倒序维护即可,当前物品选定之后,之前选定的物品就都要多加一遍,所以维护一个后缀和即可。
过程维护一个最大值。时间复杂度O(n);
Ac代码:
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
#define ll long long int
struct node
{
ll val,cnt;
}a[1540];
ll b[180000];
ll sum[180000];
int main()
{
ll n;
while(~scanf("%lld",&n))
{
for(ll i=0;i<n;i++)scanf("%lld",&a[i].val);
for(ll i=0;i<n;i++)scanf("%lld",&a[i].cnt);
ll tot=0;
for(ll i=0;i<n;i++)
{
for(ll j=0;j<a[i].cnt;j++)
{
b[tot++]=a[i].val;
}
}
sort(b,b+tot);
ll ans=0;
ll output=0;
ll sum=0;
for(ll i=tot-1;i>=0;i--)
{
sum+=b[i];
output+=sum;
ans=max(ans,output);
}
printf("%lld\n",ans);
}
}