#include <bits/stdc++.h>
using namespace std;
int main(){
int t;
cin>>t;
int arr[t];
for(int i=1;i<=t;i++)
cin>>arr[i];
sort(arr,arr+t);
int ans = 0;
for(int i=1;i<=t;i++){
int c = arr[i]*i;
ans = max(c,ans);
}
cout<<ans<<endl;
return 0;
}
Indexes in an array of size t is from 0 to t-1 only. But here you are also accessing values at index t that would give garbage value.
You should declare an array of size atleast t+1 as you are using the indexes from 1 to t.
#include <bits/stdc++.h>
using namespace std;
int main(){
long long n; cin >> n ;
vector<long long> v(n);
for(long long i = 0 ; i < n;i++){
cin >> v[i];
}
sort(v.begin(),v.end());
long long ans = -1;
for(long long i = 0 ; i < n;i++){
long long c = v[i]*((long long)v.size()-i);
ans = max(ans,c);
}
cout << ans <<endl;
return 0;
}