company

时间:2024-01-21 21:55:37

Problem 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).

 

Output

Output an integer in a single line, indicating the max total benefit.

 

Sample Input

4
-1 -100 5 6
1 1 1 2


Sample 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.

 

Source

“浪潮杯”山东省第八届ACM大学生程序设计竞赛(感谢青岛科技大学)

 

思路:

我们可以先把所有商品的价值存在数组中(包括数量大于一的),排序并求出当前的 sum,ans

然后从数组最右边开始枚举,判断是否需要加上当前商品,若加上,则 sum += ans + arr[i],因为总天数加了一天,所以后面的所有商品价值和也应该增加。

判断如果当前得到新的sum变小了,跳出,输出最大的 sum

 1 #include<algorithm>
 2 #include<iostream>
 3 #include<cstdio>
 4 using namespace std;
 5 struct Goods{
 6     long long int val,cnt;
 7     bool friend operator < (Goods a,Goods b){
 8         return a.val < b.val;
 9     }
10 }goods[1005];
11 long long int n,flag,sum,ans,idx,arr[100005];
12 int main(){
13     scanf("%lld",&n);
14     for(int i = 1;i <= n;i++)
15         scanf("%lld",&goods[i].val);
16     for(int i = 1;i <= n;i++)
17         scanf("%lld",&goods[i].cnt);
18     sort(goods + 1,goods + n + 1);
19     for(int i = 1;i <= n;i++){
20         if(goods[i].cnt != 1)
21             for(int j = 1;j <= goods[i].cnt;j++)
22                 arr[++idx] = goods[i].val;
23         else
24             arr[++idx] = goods[i].val;
25     }
26     flag = idx;
27     for(int i = 1;i <= idx;i++)
28         if(arr[i] >= 0){
29             flag = i - 1;
30             for(int k = i;k <= idx;k++){
31                 ans += arr[k];
32                 sum += arr[k] * (k - i + 1);
33             }
34             break;
35         }
36     if(flag == idx)
37         printf("%lld\n",arr[idx]);
38     else{
39         for(int i = flag;i >= 1;i--){
40             if(sum + ans + arr[i] > sum){
41                 sum += ans + arr[i];
42                 ans += arr[i];
43             }
44             else
45                 break;
46         }
47         printf("%lld\n",sum);
48     }
49     return 0;
50 }
View Code