Reorder Codeforces Round #678 SOLUTION

A. Reorder Codeforces Round #678 SOLUTION

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.
SOLUTION :
 
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. typedef long long ll;
  4. int t = 1;
  5. int n, m;
  6. int main()
  7. {
  8. ios_base::sync_with_stdio(false);
  9. cin.tie(0);
  10. cout.tie(0);
  11. cin >> t;
  12. while(t–)
  13. {
  14. ll sum = 0;
  15. cin >> n >> m;
  16. for(int i = 1; i <= n; i++)
  17. {
  18. int val;
  19. cin >> val;
  20. sum += val;
  21. }
  22. if(sum == m)
  23. cout << “YESn”;
  24. else
  25. cout << “NOn”;
  26. }
  27. return 0;
  28. }

Leave a Comment

close
error: Content is protected !!