Computer Programming Contest Preparation

ToolBox - Source for: naq/2025/a.c



/home/toolbox/public_html/solutions/naq/2025/a.c
    1 #include <stdio.h>
    2 #include <string.h>
    3 #include <sys/types.h>
    4 #include <sys/stat.h>
    5 #include <fcntl.h>
    6 #include <stdint.h>
    7 #include <math.h>
    8 #include <stdlib.h>
    9 #include <ctype.h>
   10 
   11 #define TRUE  (1 == 1)
   12 #define FALSE (1 != 1)
   13 
   14 #define DEBUG if (FALSE)
   15 
   16 /*
   17  *  Author: Isaac Traxler
   18  *    Date: 2025-10-11
   19  * Purpose: fun
   20  * Problem: naq-a
   21  */
   22 
   23 /*
   24  * This template reads data until a terminating value is reached.
   25  */
   26 
   27 #define R 0
   28 #define G 1
   29 #define B 2
   30 #define RG 0
   31 #define GB 1
   32 #define MAX 3
   33 
   34 int need[MAX];
   35 int has[MAX];
   36 int forsale[MAX-1];
   37 int buy;
   38 
   39 void init()
   40 {
   41     /* FUNCTION init */
   42 } /* FUNCTION init */
   43 
   44 void dump()
   45 {
   46     /* FUNCTION dump */
   47 } /* FUNCTION dump */
   48 
   49 void getInput()
   50 {
   51     /* FUNCTION getInput */
   52     scanf(" %d %d %d ", &need[R], &need[G], &need[B]);
   53     scanf(" %d %d %d ", &has[R], &has[G], &has[B]);
   54     scanf(" %d %d ", &forsale[RG], &forsale[GB]);
   55 } /* FUNCTION getInput */
   56 
   57 void process()
   58 {
   59     /* FUNCTION process */
   60     buy = 0;
   61     if (need[R] <= has[R])
   62         {
   63             need[R] = 0;
   64         }
   65     else
   66         {
   67             need[R] = need[R] - has[R];
   68         }
   69     if (need[G] <= has[G])
   70         {
   71             need[G] = 0;
   72         }
   73     else
   74         {
   75             need[G] = need[G] - has[G];
   76         }
   77     if (need[B] <= has[B])
   78         {
   79             need[B] = 0;
   80         }
   81     else
   82         {
   83             need[B] = need[B] - has[B];
   84         }
   85     if (forsale[RG] >= need[R])
   86         {
   87             /* buy RG */
   88             buy = need[R];
   89             forsale[RG] = forsale[RG] - need[R];
   90         } /* buy RG */
   91     else
   92         {
   93             buy = -1;
   94         }
   95     if ((0 <= buy) && (forsale[GB] >= need[B]))
   96         {
   97             /* buy GB */
   98             buy = buy + need[B];
   99             forsale[GB] = forsale[GB] - need[B];
  100         } /* buy GB */
  101     else
  102         {
  103             buy = -1;
  104         }
  105     /* can we deal with needed G */
  106     if ((0 <= buy) && ((forsale[RG] + forsale[GB]) >= need[G]))
  107         {
  108             buy = buy + need[G];
  109         }
  110     else
  111         {
  112             buy = -1;
  113         }
  114     printf("%d\n", buy);
  115 
  116 } /* FUNCTION process */
  117 
  118 int main()
  119 {
  120     /* main */
  121     int moreToDo;
  122 
  123     init();
  124     getInput();
  125     process();
  126 
  127     return EXIT_SUCCESS;
  128 } /* main */
  129