A. Reorder
time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard output
For a given array a consisting of n integers and a given integer m find if it is possible to reorder elements of the array a in such a way that ∑ni=1∑nj=iajj equals m? It is forbidden to delete elements as well as insert new elements. Please note that no rounding occurs during division, for example, 52=2.5.
Input
The first line contains a single integer t — the number of test cases (1≤t≤100). The test cases follow, each in two lines.
The first line of a test case contains two integers n and m (1≤n≤100, 0≤m≤106). The second line contains integers a1,a2,…,an (0≤ai≤106) — the elements of the array.
Output
For each test case print “YES”, if it is possible to reorder the elements of the array in such a way that the given formula gives the given value, and “NO” otherwise.
Example
inputCopy
2
3 8
2 5 1
4 4
0 1 2 3
outputCopy
YES
NO
Note
In the first test case one of the reorders could be [1,2,5]. The sum is equal to (11+22+53)+(22+53)+(53)=8. The brackets denote the inner sum ∑nj=iajj, while the summation of brackets corresponds to the sum over i.
题意
给你n个数,能不能找到一个排序,使得
的值等于m,可以输出YES,不可以输出NO
思路
我以为是顺着题目意思写结果WA一次,TLE两次
WA是直接sort排序了
TLE是加了next_permutation
然后发现其实是判断n个数的值等不等于m就行
Code
#include"iostream"
#include"cstdio"
#include"stdlib.h"
#include"cstring"
#include"cstdlib"
#include"vector"
#include"map"
#include"algorithm"
#include <iomanip>
#include <time.h>
#include <fstream>
#include <sstream>
#include <math.h>
#include<bits/stdc++.h>
//const double PI=acos(-1.0);
using namespace std;
#define mem(a,b) memset(a,b,sizeof(a))
#define lowbit(x) x&-x
#define inf 0x3f3f3f3f
#define pr pair<int,int>
#define ios ios::sync_with_stdio(false);
typedef pair<char,int> PAIR;
struct CmpByValue
{
bool operator()(const PAIR& lhs, const PAIR& rhs)
{
return lhs.second > rhs.second;
}
};
//priority_qe<int,vector<int>,greater<int>> pp;
const int mod = 1000000007;
const int M = 1e5 +100 ;
const int limt = 1<<20;
const int INF = 1e9;
typedef long long ll;
int mp[M];
double sum;
bool flag;
ll t,s,n,m,ans,cnt;
int main()
{
ios;
cin>>t;
ll k=1;
while(t--)
{
cnt=0;
cin>>n>>m;
for(int i=1; i<=n; i++)
cin>>mp[i],cnt+=mp[i];
if(cnt==m)
cout<<"YES"<<endl;
else
cout<<"NO"<<endl;
}
return 0;
}