Language: C++

#include <iostream>
#include <cmath>
#include <algorithm>
using namespace std;

template <typename T>
void swap(T *Element1, T *Element2)
{
    T temp = *Element1;
    *Element1 = *Element2;
    *Element2 = temp;
}

template <typename T>
bool check_AllValuesAreSame(const int TheNumberOfElements, T first, ...)
{
    va_list args;
    va_start(args, first);
    bool Are_AllValuesSame = true;

    for(int i=1; i<TheNumberOfElements; i++)
    {

        if(first != va_arg(args, T) )
        {
            va_end(args);
            return false;
        }
    }
    va_end(args);
    return true;
}

template <typename T>
void QuickSort_UsingIDMinAsPivot(T *Array, T *Array_Another, const int ID_Min, const int TheNumberOfElements)
{

    // if N = 1, It does not need to sort
    if(TheNumberOfElements <=1)
    {
        return;
    }
    if(check_AllValuesAreSame(TheNumberOfElements, ID_Min, Array) )
    {
        return;
    }
    const int ID_Max = TheNumberOfElements + ID_Min-1;
    int left =ID_Min, right=ID_Max;
    T Pivot = Array[ID_Min];
    int PosMax_ValueEqualToPivot = ID_Min;
    int PivotPosInArray = ID_Min;



    bool Flag_Swap;

    while(left < right)
    {
        // 1. left
        while( (Array[left] <= Pivot) && (left <= right) )
        {
            left++;
        }

        // 2. right
        while( Array[right] > Pivot && left <= right )
        {
            right--;
        }
        // 3. swap
        if(left < right)
        {
            // cout << "  swap " << left << " and " << right << endl;
            swap(&Array[left], &Array[right]);
            swap(&Array_Another[left], &Array_Another[right]);
        }

    }
    /*
    cout << "left, right = " << left << ", " << right << endl;
    cout << "move Pivot(= " << Pivot << " ) to the center: from " <<  PivotPosInArray << " to " << right << endl;
    */
    // finally, place the pivot at the center (=left)
    swap(&Array[right], &Array[PivotPosInArray]);
    swap(&Array_Another[right], &Array_Another[PivotPosInArray]);

    // recursive call
    // cout << "1div: left side" << endl;
    QuickSort_UsingIDMinAsPivot(Array, Array_Another, ID_Min, right-ID_Min);
    // cout << "2div: right side" << endl;
    QuickSort_UsingIDMinAsPivot(Array, Array_Another, right+1, ID_Max-right);


}


template <typename T>
void InsertionSort(T *Array, T *Array_AnotherKey, const int ID_Min, const int TheNumberOfElements)
{
    // 1. search the element which satisfies > Array[i]
    // Array[0:i-1] is already sorted.
    for(int i=1; i< TheNumberOfElements; i++)
    {

        T tmp = Array[i]; // add not sorted data
        // A. check whether Array[i] places properly or need to change position?
        if(Array[i-1] > tmp)
        {
            // B. change position
            int j = i;
            do
            {
                Array[j] = Array[j-1];
                Array_AnotherKey[j] = Array_AnotherKey[j-1];
                j--;
            }
            while( j>0 && Array[j-1] > tmp); // if Array[j-1] >= tmp, stability will be lost.
        }

    }
} // end



int main(void)
{

    unsigned long long int K_TrainingTimes;
    unsigned long int tmp_i;

    // 1. input
    cin >> tmp_i >> K_TrainingTimes;
    const unsigned long int N_Member = tmp_i;

    long int A_Cost[N_Member], F_FoodResistance[N_Member], AF_Time[N_Member];
    for(unsigned long int i= 0; i < N_Member; i++)
    {
        cin >> A_Cost[i];
    }
    for(unsigned long int i= 0; i < N_Member; i++)
    {
        cin >> F_FoodResistance[i];
        AF_Time[i] = A_Cost[i] * F_FoodResistance[i];
    }

    // cout << "Check: k, N = " << K_TrainingTimes << ", " << N_Member << endl;

    // 2. sort array
    QuickSort_UsingIDMinAsPivot(AF_Time, F_FoodResistance, 0, N_Member);
    
    while( K_TrainingTimes >0)
    {
        if(AF_Time[N_Member-1] ==0)
        {
            break;
        }
        int TrainingTimes = (int) (ceil) ( ( AF_Time[N_Member-1]-AF_Time[N_Member-2] ) / F_FoodResistance[N_Member-1]);
        if(K_TrainingTimes < TrainingTimes )
        {
            TrainingTimes = K_TrainingTimes;
        }
        if(TrainingTimes <= 0)
        {
            TrainingTimes = 1;
        }
        AF_Time[N_Member-1] -= F_FoodResistance[N_Member-1]* TrainingTimes;

        if(AF_Time[N_Member-1] <0)
        {
            AF_Time[N_Member-1] = 0;
        }
        InsertionSort(AF_Time, F_FoodResistance, 0, N_Member);

        K_TrainingTimes -= TrainingTimes;

    }
    cout << AF_Time[N_Member-1] << endl;



    return 0;
}


Compiler output:
a.cc: In function 'bool check_AllValuesAreSame(int, T, ...)':
a.cc:24:35: error: expected primary-expression before ')' token
   24 |         if(first != va_arg(args, T) )
      |                                   ^
a.cc:24:21: error: there are no arguments to 'va_arg' that depend on a template parameter, so a declaration of 'va_arg' must be available [-fpermissive]
   24 |         if(first != va_arg(args, T) )
      |                     ^~~~~~
a.cc:24:21: note: (if you use '-fpermissive', G++ will accept your code, but allowing the use of an undeclared name is deprecated)
a.cc:26:13: error: there are no arguments to 'va_end' that depend on a template parameter, so a declaration of 'va_end' must be available [-fpermissive]
   26 |             va_end(args);
      |             ^~~~~~
a.cc:30:5: error: there are no arguments to 'va_end' that depend on a template parameter, so a declaration of 'va_end' must be available [-fpermissive]
   30 |     va_end(args);
      |     ^~~~~~
a.cc: In instantiation of 'bool check_AllValuesAreSame(int, T, ...) [with T = int]':
a.cc:43:30:   required from 'void QuickSort_UsingIDMinAsPivot(T*, T*, int, int) [with T = long int]'
   43 |     if(check_AllValuesAreSame(TheNumberOfElements, ID_Min, Array) )
      |        ~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
a.cc:149:32:   required from here
  149 |     QuickSort_UsingIDMinAsPivot(AF_Time, F_FoodResistance, 0, N_Member);
      |     ~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
a.cc:18:13: error: 'va_start' was not declared in this scope
   18 |     va_start(args, first);
      |     ~~~~~~~~^~~~~~~~~~~~~
a.cc:24:35: error: 'va_arg' was not declared in this scope
   24 |         if(first != va_arg(args, T) )
      |                                   ^
a.cc:26:19: error: 'va_end' was not declared in this scope
   26 |             va_end(args);
      |             ~~~~~~^~~~~~
a.cc:30:11: error: 'va_end' was not declared in this scope
   30 |     va_end(args);
      |     ~~~~~~^~~~~~