Problem A:Greedy Monocarp solution updated||Educational Codeforces Round 172(Div.2)

Поділитися
Вставка
  • Опубліковано 29 гру 2024

КОМЕНТАРІ • 1

  • @computerknowledge4217
    @computerknowledge4217  27 днів тому

    //Problem A
    //Greedy Monocarp solutioncorrected..here
    //In C++ language
    #include
    #include
    #include
    using namespace std;
    // Comparator function for sorting in descending order
    bool compare(int first, int second) {
    return first > second;
    }
    void processTestCase() {
    int totalElements, threshold;
    cin >> totalElements >> threshold;
    // Allocate memory for the elements
    vector elements(totalElements);
    for (int index = 0; index < totalElements; index++) {
    cin >> elements[index];
    }
    // Sort the array in descending order
    sort(elements.begin(), elements.end(), compare);
    int accumulatedSum = 0;
    for (int index = 0; index < totalElements; index++) {
    accumulatedSum += elements[index];
    if (accumulatedSum > threshold) {
    accumulatedSum -= elements[index];
    cout