// Cody McMahon & Chris Washburn
//  9/23/11
// Friday Ballons Problem

#include <stdio.h>
#include <math.h>

int main() {

    //Variables:teams is the number of teams per case which also relates to the number of lines,
    //numA = balloons in room A per case, numB = balloons in room B per case,

    int num;
    int teams;
    int numA;
    int numB;
    int i, j, k;
    int dif=0;
    int teamNum;
    int teamBalloons=0;
    int total_distance=0;

    //create pointers to needed files
    FILE*fin=fopen("balloons.in","r");

    FILE*fout=fopen("balloons-cody.out","w");

   //the while loop always runs because num is true so I can go through
   //every case with the check if all three of the scanned in integers for
   //that row is zero exit the loop because that is how the document ends.
   while(1){

        fscanf(fin, "%d%d%d", &teams, &numA, &numB);

        if(teams==0&&numA==0&&numB==0)
            break;

        //for each case the number of rows is decided by the number of teams
        //and each row has three columns so all of each teams data is stored in the
        //array with the for loop.
        int* array = (int*)malloc(sizeof(int)*(3*teams));
        //int array[(teams*3)];

        for(k=0; k<teams*3;k++)
        {

            fscanf(fin, "%d", &num);
            array[k]= num;
        }

        //the outer for loop runs as many times as there are teams
        //so that way all of their distances are added.
        for(j=0; j<teams; j++){

            //the inner for loop checks to see which of the teams has
            // greates difference and stores its team number which is the
            //slot in the array that holds that teams number of balloons
            for(i=1; i<teams*3; i+=3){
                if (array[i]!=-1 && abs((array[i]-array[i+1]))>=dif){
                    dif=abs((array[i]-array[i+1]));
                    teamNum=i-1;
                }
            }

            //the if statement checks to see which room the team is closer to
            //and the following while statements start with the room it is closes to
            //provided there are still balloons avaible and the while's run
            //as long as the room has balloons and the teams aren't full
            //each time it runs it adds that distance to the total.
            if ((array[teamNum+1])<(array[teamNum+2])){

                while(numA>0 && teamBalloons<(array[teamNum])){
                    total_distance += (array[teamNum+1]);
                    numA--;
                    teamBalloons++;
                }

                while(numB>0 && teamBalloons<(array[teamNum])){
                    total_distance += (array[teamNum+2]);
                    numB--;
                    teamBalloons++;
                }
            }
            else{

                while(numB>0 && teamBalloons<(array[teamNum])){
                    total_distance += (array[teamNum+2]);
                    numB--;
                    teamBalloons++;
                }

                while(numA>0 && teamBalloons<(array[teamNum])){
                    total_distance += (array[teamNum+1]);
                    numA--;
                    teamBalloons++;
                }
            }

            //reset dif and teamBalloons for the teams checks
            //change the current teams data so it can never be used again
            //checked in the if statement for the biggest differece
            dif=0;
            teamBalloons=0;
            array[teamNum]=-1,array[teamNum+1]=-1,array[teamNum+2]=-1;
        }

        //print each cases total distance and reset to prepare for the next one
        fprintf(fout,"%d\n",total_distance);
        total_distance=0;

        free(array);
    }

    fclose(fin);
    fclose(fout);
    return 0;
}
