Lotus and Characters
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 262144/131072 K (Java/Others)Total Submission(s): 181 Accepted Submission(s): 70
Problem Description
Lotus has
n
kinds of characters,each kind of characters has a value and a amount.She wants to construct a string using some of these characters.Define the value of a string is:its first character's value*1+its second character's value *2+...She wants to calculate the maximum value of string she can construct.
Since it's valid to construct an empty string,the answer is always ≥0 。
Since it's valid to construct an empty string,the answer is always ≥0 。
Input
First line is
T(0≤T≤1000)
denoting the number of test cases.
For each test case,first line is an integer n(1≤n≤26) ,followed by n lines each containing 2 integers vali,cnti(|vali|,cnti≤100) ,denoting the value and the amount of the ith character.
For each test case,first line is an integer n(1≤n≤26) ,followed by n lines each containing 2 integers vali,cnti(|vali|,cnti≤100) ,denoting the value and the amount of the ith character.
Output
For each test case.output one line containing a single integer,denoting the answer.
Sample Input
2 2 5 1 6 2 3 -5 3 2 1 1 1
Sample Output
35 5
/*分析:按照从小到大排序从大于等于0开始计算是一种错误的思路,有的数据需要负数才能获得最大值。
例如:
2
3
-1 3
2 1
1 1
2
-1 5
4 2
答案分别为8和37
*/
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
#include<queue>
#include<stack>
#include<iostream>
#include<algorithm>
using namespace std;
struct node{
int c,v;
}a[30];
int cmp(node a,node b)
{
return a.v<b.v;
}
int main()
{
int t,n;
int index;
int sum,sum1,add,flag;
scanf("%d",&t);
while(t--)
{
index=1;sum=flag=0;
scanf("%d",&n);
for(int i=0;i<n;i++)
scanf("%d%d",&a[i].v,&a[i].c);
sort(a,a+n,cmp);
for(int i=0;i<n;i++)
{
if(a[i].v>=0)
{
if(!flag)
{
flag=1;
add=i;
}
for(int j=0;j<a[i].c;j++)
{
sum+=a[i].v*(index++);
}
}
}
//逐个把负数算进去,直到sum开始呈下降趋势
while(add>=0)
{
for(int k=1;k<=a[add].c;k++)
{
index=1;sum1=0;
for(int l=0;l<k;l++)
{
sum1+=a[add].v*(index++);
}
for(int i=add+1;i<n;i++)
{
for(int j=0;j<a[i].c;j++)
{
sum1+=a[i].v*(index++);
}
}
if(sum<sum1)
sum=sum1;
else
break;
}
add--;
}
printf("%d\n",sum);
}
return 0;
}