as min and n-1 both are int so their product will also be an int and it will be assigned to ans.
and in the product overflow will happen as its int only

int cases;
scanf("%d", &cases);
while (cases--) {
int num;
scanf("%d", &num);
int arr[num];
int min = INT_MAX;
for (int i = 0 ; i < num ; i++) {
scanf("%d", &arr[i]);
if (min > arr[i]) {
min = arr[i];
}
}
printf("%d\n", min * (num - 1));
}
return 0;

}

What’s wrong with this? I am not able to pass third cases. I used long long but it shows runtime overflow. Please help

Hello Karthik ,I think your logic is little wrong.
Let say your array is as shown below.
9 1 2 5 3 0 – - the ans should be 0 but your code is gives 4.
I think your code is working fine if the smallest number lies within 0th or 1st index(as in case of the provided Example test case), It fails once the smallest element is placed in 2nd position of array and so on.

your ans is only finding the cost, but in question it is given that we have to find the minimum cost , see…
suppose 5, 4, 3 are the numbers so your solution will give ( 5 > 4) = 4, ( 4 > 3 ) = 3
so cost = 4 + 3 = 7
but the minimum cost must be 6 as (4 > 3) = 3 , and (5 > 3) = 3
cost( min ) = 3+3 =6
so better first you find the smallest element.

#include<bits/stdc++.h>
using namespace std;
int main()
{
int t;
cin>>t;
while(t–)
{

long long int n;
cin>>n;
long long int a[n],i,sum=0;
for(i=0;i<n;i++)
{
cin>>a[i];
}
long long int min = a[0];
for(i=1;i<n;i++)
{
if(min>a[i])
min = a[i];
sum+=min;
}
cout<<sum<<endl;
}
return 0;

I think there is one trick to solve this problem very easily, for that we first have to store the elements in vector or array, than we have to find the element with minimum value and the answer will be minimum element value multiplied by the size of vector - 1 #include

int t;
vector <int> vec;
cin>>t;
while (t--)
{
int n,e;
cin>>n;
for (int i = 0; i < n; i++)
{
cin>>e;
vec.push_back(e);
}
int min;
cout<<vec[0]*(vec.size()-1)<<endl;
vec.clear();
}
return 0;

Java integer supports range from -2147483648 to 2147483647
And given constraints for Elements of Array was 1 ≤ Ai ≤ 10^5 which can easily be stored in an Integer type Array, but no, it was giving wrong answer, but when i switched to Long type Array, my answer get Accepted.
Can anyone tell me whats wrong here, the constraints were false or there is any other problem?

Aside from your TLE issue, this code will not yield the required results as you are not able to identify the minimum cost for the operations.

Given in this array [5,7,2,4,1] the minimum cost should be 4 because:
[5,7,2,4,1] = sum += min(4,1) – which is 1
[5,7,2,1] = sum += min(2,1) – which is 1
[5,7,1] = sum += min(7,1) – which is 1
[5,1] = sum += min(5,1) – which is 1

Therefore sum = 4
Try to refactor your code to identify which is the best pair to calculate first