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
- #include <cstdio>
#include <iostream>
#include <cstring>
#include <algorithm>
#include <cstdlib>
using namespace std;
int val[1005], cnt;
int v[100050];
int main()
{
int n;
while(~scanf("%d", &n))
{
memset(v, 0, sizeof(v));
memset(val, 0, sizeof(val));
long long t = 1;//统计的时候天数
for(int i = 1; i <= n; i++)
scanf("%d", &val[i]);//输入商品的单价
for(int i = 1; i <= n; i++)
{
scanf("%d", &cnt);
for(int j = t ; j < t + cnt; j ++)
v[j] = val[i]; //所有对应时间段内赋予当前单价的值 v数组即是按照单价和数量展开后的总得数组
t += cnt;//时间求和
}
t --;//减去最开始的1
sort(v+1, v + t+1);//v数组进行升序排序
long long ans = 0;
long long sum = 0;//求和的数值定义为long long类型防止爆数
for(int i = t; i >= 1; i--)
{
if( v[i] + sum < 0)//跳过会使总价值变小的物品
continue;
else
{
ans += v[i] + sum;//通过这里对sum的运用 从而使天数乘以价值能够做都做到动态化的过程 而不再是单价乘以天数
sum += v[i];//sum也用于比较筛选会使总价值变小的物品
}
}
cout << ans <<endl;
}
return 0;
}