#include
using namespace std;
int main() {
int n ,i,sum =0 ;
cin>> n ;
while (n--)
{
int elephant ,candies ;
int arr[elephant];
cin >> elephant >>candies ;
for (i = 0 ;i< elephant ; i++)
{
cin >>arr[i] ;
sum += arr[i];
}
if (sum <= candies)
cout << "Yes" << endl ;
else
cout << "No" << endl ;
}
return 0;
}
First of all you have to declare sum inside the loop or update it to 0 in every iteration. And take the size of sum to long long so that if the sum becomes larger than int largest value, it does not overflow.
You can refer to my solution to understand better.
#include <iostream>
using namespace std;
#define ll long long
int main() {
// your code goes here
int test;
cin >> test;
while(test--){
int N, K;
cin >> N >> K;
ll sum = 0;
ll arr[N];
for(int i = 0 ;i < N ; i++){
cin >> arr[i];
sum += arr[i];
}
if(sum <= K){
cout << "Yes\n";
}
else{
cout << "No\n";
}
}
return 0;
}