company
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
Idea: first add all values to a vector, and then sort from small to large, because the title requires that items must be sold continuously, so we must put the negative value in the first place to sell (if we want to sell negative value items ), all the remaining positive-value items are sold in order from small to large. The bigger the value, the bigger the selling value will be, because the actual selling price of each item is multiplied by the number of days, so this question I only need Consider which items with negative values can be added as much as possible, because the items after the items with negative values in the front will move backward in turn, and the number of days will increase by 1, so for each additional item with a negative value, the item with a positive value will be added. Shifting all back by one day is equivalent to adding an extra sum of all positive values. So we only need to judge that when the negative value is sold on the kth day, as long as the value subtracted on this day is less than the sum of the positive values, then the total value must increase.
Because each increase of a negative value item is equivalent to increasing the value of sum-a[i]*k, as long as a[i]*k<sum, sum-a[i]*k is greater than zero, and the total value can be increased
code:
#include <bits/stdc++.h> using namespace std; typedef long long ll; vector<int> a; int main(){ int n; scanf("%d",&n); int q; for(int i = 0; i < n; i++){ scanf("%d",&q); a.push_back(q); } for(int i = 0; i < n; i++){ scanf("%d",&q); if(q > 1){ for(int j = 0; j < q - 1; j++){ a.push_back(a[i]); } } } sort(a.begin(),a.end()); int pos = upper_bound(a.begin(),a.end(),0) - a.begin(); ll sum = 0; for(int i = pos; i < a.size(); i++){ sum += a[i]; } bool ok; int record; for(int i = 0; i < pos; i++){ int k = 1; for(int j = i; j < pos; j++){ ll sum2 = 0; ok = 0; sum2 += a[j] * k; k++; if(abs(sum2) > sum){ ok = 1; break; } } if(ok == 0){ record = i; break; } } ll ans = 0; int cnt = 1; for(int i = record; i < a.size(); i++){ ans += a[i] * cnt; cnt++; } printf("%lld\n",ans); return 0; }