Created
November 5, 2023 03:41
-
-
Save RiyaadHossain/9bdee7432fa6b1052d06818e8bf841b6 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
void sort(int *arr, int n) | |
{ | |
for (int i = 0; i < n - 1; i++) | |
{ | |
int targetIdx = i; | |
for (int j = i + 1; j < n; j++) | |
{ | |
if (targetIdx < arr[j]) | |
targetIdx = j; | |
} | |
int temp = arr[targetIdx]; | |
arr[targetIdx] = arr[i]; | |
arr[i] = temp; | |
} | |
} | |
int main() | |
{ | |
int n, k; | |
scanf("%d %d", &n, &k); | |
int arr[n]; | |
for (int i = 0; i < n; i++) | |
scanf("%d", &arr[i]); | |
sort(arr, n); | |
long long maxSum = 0; | |
for (int i = 0; i < k; i++) | |
{ | |
if (arr[i] > 0) | |
maxSum += arr[i]; | |
} | |
printf("%lld", maxSum); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment