Computer Programming Contest Preparation

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



/home/toolbox/public_html/solutions/109/10954/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 
   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         } /* load array */
   72     return (dataReadFlag);
   73 } /* FUNCTION getInput */
   74 
   75 void process()
   76 {
   77     /* FUNCTION process */
   78     long long int cost = 0;
   79     long long int tot = 0;
   80     int i;
   81 
   82     qsort(a, cnt, sizeof(int), compare);
   83     for (i=1; i<cnt; i++)
   84         {
   85             /* for */
   86             a[i] = a[i] +  a[i-1];
   87             cost = cost + a[i];
   88             qsort(&a[i], cnt-i, sizeof(int), compare);
   89         } /* for */
   90     printf("%lld\n", cost);
   91 } /* FUNCTION process */
   92 
   93 int main()
   94 {
   95     /* main */
   96     int moreToDo;
   97 
   98     init();
   99     moreToDo = getInput();
  100     while (moreToDo)
  101         {
  102             /* while */
  103             process();
  104             moreToDo = getInput();
  105         } /* while */
  106 
  107     return EXIT_SUCCESS;
  108 } /* main */
  109