A. Reorder Codeforces Round #678 SOLUTION
For a given array aa consisting of nn integers and a given integer mm find if it is possible to reorder elements of the array aa in such a way that ∑ni=1∑nj=iajj∑i=1n∑j=inajj equals mm? It is forbidden to delete elements as well as insert new elements. Please note that no rounding occurs during division, for example, 52=2.552=2.5.Input
The first line contains a single integer tt — the number of test cases (1≤t≤1001≤t≤100). The test cases follow, each in two lines.
The first line of a test case contains two integers nn and mm (1≤n≤1001≤n≤100, 0≤m≤1060≤m≤106). The second line contains integers a1,a2,…,ana1,a2,…,an (0≤ai≤1060≤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
input
2 3 8 2 5 1 4 4 0 1 2 3
output
YES NO
Note
In the first test case one of the reorders could be [1,2,5][1,2,5]. The sum is equal to (11+22+53)+(22+53)+(53)=8(11+22+53)+(22+53)+(53)=8. The brackets denote the inner sum ∑nj=iajj∑j=inajj, while the summation of brackets corresponds to the sum over ii.
Solution
Program C++:
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef long double ld; signed main() { ios::sync_with_stdio(0); cin.tie(0); int t; cin >> t; while (t--) { int n, s = 0, w; cin >> n >> w; for (int i = 1; i <= n; i++) { int x; cin >> x; s += x; } if (s == w) { cout << "YES\n"; } else { cout << "NO\n"; } } }
#include <iostream> using namespace std; int main() { int a[100], t, n, m, i, s, b; cin >> t; for (i = 0; i < t ; i++){ cin>>n>>m; s = 0; for (int j = 0; j < n; j++) { cin >> b; s += b; } cout << (s == m ? "YES" : "NO") << endl; } return 0; }