Computer Programming Contest Preparation

ToolBox - Source for: 109/10954/b.c



/home/toolbox/public_html/solutions/109/10954/b.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 
   10 #define TRUE  (1 == 1)
   11 #define FALSE (1 != 1)
   12 
   13 #define DEBUG if (FALSE)
   14 
   15 /*
   16  *  Author: Isaac Traxler
   17  *    Date: 2016-09-06
   18  * Purpose: fun
   19  * Problem: 10954 - Add All
   20  */
   21 
   22 /*
   23  * This template reads data until a terminating value is reached.
   24  */
   25 
   26 #define MAX 5001
   27 
   28 int cnt;
   29 int a[MAX];
   30 
   31 void init()
   32 {
   33     /* FUNCTION init */
   34 } /* FUNCTION init */
   35 
   36 void dump()
   37 {
   38     /* FUNCTION dump */
   39     int i;
   40 
   41     for (i=0; i<cnt; i++)
   42         {
   43             printf("%d ", a[i]);
   44         }
   45     printf("\n");
   46 } /* FUNCTION dump */
   47 
   48 int compare(const void *a, const void *b)
   49 {
   50     /* FUNCTION compare */
   51     return ( *(int*)a - *(int*)b );
   52 } /* FUNCTION compare */
   53 
   54 
   55 int getInput()
   56 {
   57     /* FUNCTION getInput */
   58     int dataReadFlag;
   59     int i;
   60 
   61     scanf(" %d ", &cnt);
   62     dataReadFlag = (0 != cnt);
   63     if (dataReadFlag)
   64         {
   65             /* load array */
   66             for (i=0; i<cnt; i++)
   67                 {
   68                     /* for */
   69                     scanf(" %d ", &a[i]);
   70                 }  /* for */
   71             DEBUG dump();
   72             qsort(a, cnt, sizeof(int), compare);
   73             DEBUG dump();
   74         } /* load array */
   75     return (dataReadFlag);
   76 } /* FUNCTION getInput */
   77 
   78 void process()
   79 {
   80     /* FUNCTION process */
   81     long long int cost = 0;
   82     long long int tot = 0;
   83     int i;
   84 
   85     tot = a[0];
   86     for (i=1; i<cnt; i++)
   87         {
   88             /* for */
   89             DEBUG printf("before: a[%d] = %d   tot = %d   cost = %d\n", i, a[i], tot, cost);
   90             tot = tot + a[i];
   91             cost = cost + tot;
   92             DEBUG printf("after:  a[%d] = %d   tot = %d   cost = %d\n", i, a[i], tot, cost);
   93         } /* for */
   94     printf("%lld\n", cost);
   95 } /* FUNCTION process */
   96 
   97 int main()
   98 {
   99     /* main */
  100     int moreToDo;
  101 
  102     init();
  103     moreToDo = getInput();
  104     while (moreToDo)
  105         {
  106             /* while */
  107             process();
  108             moreToDo = getInput();
  109         } /* while */
  110 
  111     return EXIT_SUCCESS;
  112 } /* main */
  113