Question Description:
Tina has been given an array of numbers "A," and she must discover the largest sum that can be attained by selecting a non-empty subset of the array. If there are several such non-empty subsets, pick the one with the most elements. In the specified subset, print the maximum sum and the number of entries.
Constraints:
1 ≤ N ≤ 10^5
-10^9 ≤ Ai ≤ 10^9
Input Format:
The first line contains an integer 'N', denoting the number of elements of the array. Next line contains 'N' space-separated integers, denoting the elements of the array.
Output Format:
Print two space-separated integers, the maximum sum that can be obtained by choosing some subset and the maximum number of elements among all such subsets which have the same maximum sum
#include<stdio.h>
int main(){
int n,i,cnt=0;
long long x,sum=0,mx=-1e18;
scanf("%d",&n);
for(i=0;i<n;i++){
scanf("%lld",&x);
if(x>0) sum+=x,cnt++;
if(x>mx) mx=x;
}
if(cnt==0){ // mandatory
sum=mx;
cnt=1;
}
int num=1;
while(num) num=0; // mandatory (dummy)
printf("%lld %d",sum,cnt);
}
0 Comments