Skip to content

Instantly share code, notes, and snippets.

@RiyaadHossain
Created November 5, 2023 03:41
Show Gist options
  • Save RiyaadHossain/9bdee7432fa6b1052d06818e8bf841b6 to your computer and use it in GitHub Desktop.
Save RiyaadHossain/9bdee7432fa6b1052d06818e8bf841b6 to your computer and use it in GitHub Desktop.
#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