山东省第八届 ACM 省赛 company (贪心、水)

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.


Example Input

4
-1 -100 5 6
1 1 1 2


Example Output

51


题意

n 种商品以及这些商品的价值与数量,每天只能购买一件商品,且 ans sum(ti×vali) ,其中 ti 为第几天,求最终所能得到的最大 ans


思路

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

然后从数组最左边开始枚举,判断是否需要去除当前商品,若去除,则 ans-=a[i]-sum[top-1]+sum[i] ,因为总天数减少了一天,所以后面的所有商品价值和也应该减少。

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


AC 代码

#include<iostream>
#include<algorithm>
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<iostream>
using namespace std;
#include<queue>
#include<map>
#define eps (1e-8)
typedef long long LL;
const int mod = 1e9+7;
const int maxn = 100000;
int a[maxn],sum[maxn],d[maxn];
int main()
{
    int n;
    while(~scanf("%d",&n))
    {
        int top=n;
        for(int i=0; i<n; i++)
            scanf("%d",a+i);
        for(int i=0; i<n; i++)
        {
            scanf("%d",d+i);
            for(int j=1; j<d[i]; j++)
                a[top++]=a[i];
        }
        sort(a,a+top);
        sum[0]=a[0];
        LL ans=a[0];
        for(int i=1; i<top; i++)
        {
            sum[i]=sum[i-1]+a[i];
            ans+=(i+1)*a[i];
        }
        for(int i=0; i<top; i++)
        {
            LL s=ans-a[i]-sum[top-1]+sum[i];
            if(s>ans)
                ans=s;
            else break;
        }
        cout<<ans<<endl;
    }
    return 0;
}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值