UVa 11389 – The Bus Driver Problem

#include <iostream>
#include <algorithm>

using namespace std;

int main(){
    int n,d,r,i,overtime,temp,morning[105],evening[105];

    while (true){
        cin>>n>>d>>r;
        if (!n || !d || !r)return 0;

        for (i = 0 ; i < n ; i ++)
            cin>>morning[i];
        for (i = 0 ; i < n ; i ++)
            cin>>evening[i];

        sort(morning,morning+n);
        sort(evening,evening+n);

        overtime = 0;
        for (i = 0 ; i < n ; i ++){
            temp = morning[i]+evening[n-i-1];
            if (temp > d)
                overtime+=r*(temp-d);

        }
        cout<<overtime<<endl;

    }

    return 0;
}