问题 J: company
时间限制: 1 秒 内存限制: 64 MB提交: 86 解决: 27
题目描述
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 pricechanges, 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 untilyou can't or don't want to do so. If you are allowed to leave some goodsunsold, what's the max total benefit you can get in the end?
输入
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 an integer in a single line, indicating the maxtotal benefit.
Hint: sell goods whose price with order as -1, 5, 6, 6, thetotal benefit would be -1*1 + 5*2 + 6*3 + 6*4 = 51.
样例输入
4
-1 -100 5 6
1 1 1 2
样例输出
51
提示
题意:
第二行给定n个数,第三行给定对应每个数的个数。然后选出几个数按一定的顺序排好,最后每个数乘以所在的位置求和。输出最大和。
思路:
把所有数存入数组中,然后从大到小排序,求出最大的前缀和即为答案。
如此题样例:
排序后 6 6 5 -1 -100
一个数 6
两个数 6*2 + 6 = (6+6)+ 6
三个数 6*3 + 6*2 + 5 = (6+6+6) + (6+6) + 5
。。。。。。
此类递增相乘的题要想到前缀和
代码:
#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
long long s[100005];
int main()
{
int n;
int pos=0;
scanf("%d",&n);
int cnt=n;
for(int i=0;i<n;i++)
{
scanf("%lld",&s[i]);
if(s[i]<0)pos++;
}
for(int i=0;i<n;i++)
{
int a;
scanf("%d",&a);
if(a>1)
{
for(int j=2;j<=a;j++)
{
s[cnt]=s[i];
cnt++;
if(s[i]<0)
pos++;
}
}
}
sort(s,s+cnt);
long long sum=0,ans=0;
long long p=0;
for(int i=cnt-1;i>=0;i--)
{
p+=s[i];
sum+=p;
ans=ans>sum?ans:sum;
}
printf("%lld\n",ans);
return 0;
}