company

company

Time Limit: 1000MS Memory Limit: 65536KB
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 ival.
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
“浪潮杯”山东省第八届ACM大学生程序设计竞赛(感谢青岛科技大学)

贪心,倒序维护即可,当前物品选定之后,之前选定的物品就都要多加一遍,所以维护一个后缀和即可。

过程维护一个最大值。时间复杂度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
****************************************************/


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值