company

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

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.

Sample Input

4
-1 -100 5 6
1 1 1 2

Sample Output

51

题解:

题意很简单,公司出售商品,第一天出售得到的利润是 v*1,第二天的是v*2,第三天的是v*3......以此类推,第n天的就是v*n。每天只可以出售一件,直到你不再想出售,算最大的总利润。

我们要做到就是看哪天该出售哪件商品,我用的方法就是把所有的商品价值v都放在一个数组里,这样就不用再管他的数量了,然后对它排序,找到它是正数的位置,把所有的正利润全部算出来,再往前推那些不是正数的部分。

注意:要用long long int !!!结果超int!这个地方我W了无数次!!

代码如下:

 
 
#include <iostream>
#include <algorithm>
#include <math.h>
#include <cstring>
#include <cstdio>
#include <cstdlib>
using namespace std;
int v[111111],c[111111];
int main()
{
    long long int n,m;
    cin >> n;
    m=n;
    for (int i=0; i<=n-1; i++)
        cin >> v[i];
    for(int i=0; i<=n-1; i++)
    {
        cin >> c[i];
        for(int j=1; j<c[i]; j++)
            v[m++]=v[i];                     //把不止一件的商品都放在v数组里
    }
    sort (v,v+m);
    n=m;
    long long int sum=0,ans,k=-1,s=0;
    for(int i=0; i<=n-1; i++)
    {
        if(v[i]>=0)
        {
            k=i;                             //找到正数位置记录下来
            break ;
        }
    }
    if(k>=0)
    {
        for (int i=k; i<=n-1; i++)
        {
            sum += v[i]*(i-k+1);             //所有正数利润的总和
            s+=v[i];                        
        }
        ans=sum ;
        for(int i=k-1; i>=0; i--)
        {
            sum+=v[i]+s;                     //往前推一,加上前一个商品的利润,而后边的商品会因为天
                                             //数变化多加一天的利润,s就是这往前推的一天多出来的利润
            if(sum > ans )                   
            {
                ans=sum;
                s+=v[i];
            }
            else                             //如果不满足,直接跳出循环,再往前推也没有意义了
                break ;
        }
        cout << ans <<endl;
    }
    else 
        cout << 0 << endl;                 //没有正数,利润输出 0 
    return 0;
}






猜你喜欢

转载自blog.csdn.net/m_y_y_/article/details/79860529