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.
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
题意:某公司有n种物品,每种物品的价值为vali,数量为cnti,在第k天卖物品i,公司将获益k*val i,并且必须天天买,如果有一天中断,将不再卖东西,求公司可以获得的最大收益
思路:可以用贪心的思想来解决此问题,val为正的物品肯定要卖出,收益才会增大,而且先卖val小的,后卖大的,
对于val为负数的物品,卖与不卖要考虑最后收益会不会增大
代码:
#include<cstdio> #include<cstring> #include<algorithm> #include<vector> using namespace std; typedef long long LL; const int N = 100005; vector<LL>vec1,vec2; LL arr[N]; int main(){ int n; scanf("%d",&n); for(int i=1;i<=n;i++) scanf("%lld",&arr[i]); for(int i=1;i<=n;i++){ int x; scanf("%d",&x); for(int j=0;j<x;j++){ if(arr[i]>=0) vec1.push_back(arr[i]); else vec2.push_back(arr[i]); } } sort(vec1.begin(),vec1.end()); sort(vec2.begin(),vec2.end()); LL ans=0,sum1=0; for(int i=0;i<vec1.size();i++){ sum1+=vec1[i]; ans+=vec1[i]*(i+1); } // printf("ans: %d\n",ans); LL per[N]={0}; LL sum[N]={0}; for(int i=vec2.size()-1;i>=0;i--) per[i]=per[i+1]+vec2[i]; for(int i=vec2.size()-1;i>=0;i--) sum[i]=sum[i+1]+per[i]; LL maxt=0; int k=1; for(int i=vec2.size()-1;i>=0;i--){ maxt=max(maxt,sum1*k+sum[i]); k++; } printf("%lld\n",ans+maxt); return 0; }