Lotus and Characters
Since it's valid to construct an empty string,the answer is always ≥0。
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)
Output
2 2 5 1 6 2 3 -5 3 2 1 1 1Sample Output
35 5
解釋一下測試數據:每次的價值數據都要先進行排序,選出價值大的放在前面
比如第一組測試數據 5 1 6 2;顯然6的價值更高,因此把6放在後面,就是6*2+6*3;再是5,就是5*1,結果就爲35,再是第二組數據:排好序後就變成2 1 -5;
應該就是2*2+1*1=5;如果是2*3+1*2+(-5)*1的話值是小於0的,因此-5數據直接捨棄了。
已經AC過的代碼:
#include<cstdio>
#include<iostream>
#include<algorithm>
using namespace std;
pair<int,int> p[35];
int main()
{
int t,n,v,c;
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
for(int i=0;i<n;i++)
{
scanf("%d %d",&v,&c);
p[i]=make_pair(v,c);
}
long long sum=0;
long long ans=0;
sort(p,p+n);
for(int i=n-1;i>=0;i--)
{
for(int j=0;j<p[i].second;j++)
{
ans+=p[i].first;
if(ans<0)
break;
sum+=ans;
}
}
cout<<sum<<'\n';
}
return 0;
}
//用到了一些pair函數的使用,此前的博客裏已經說明過了怎麼使用。