Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Resolução UVa 10954 - Thulio Guilherme
- #include <stdio.h>
- void quick_sort(int *a, int left, int right);
- int main(){
- int num, count;
- while(1){
- scanf("%d", &num);
- if(num==0) return 0;
- int array[num];
- for(count = 0; count < num; count++){
- scanf("%d", &array[count]);
- }
- int total = 0, cost = 0;
- int i, j;
- while(num != 1){
- quick_sort(array, 0, num - 1);
- total = array[num - 2] + array[num -1];
- cost = cost + total;
- array[num - 1] = '\0';
- array[num - 2] = total;
- num--;
- }
- printf("%d\n", cost);
- }
- return 0;
- }
- void quick_sort(int *a, int left, int right) {
- int i, j, x, y;
- i = left;
- j = right;
- x = a[(left + right) / 2];
- while(i <= j) {
- while(a[i] > x && i < right) {
- i++;
- }
- while(a[j] < x && j > left) {
- j--;
- }
- if(i <= j) {
- y = a[i];
- a[i] = a[j];
- a[j] = y;
- i++;
- j--;
- }
- }
- if(j > left) {
- quick_sort(a, left, j);
- }
- if(i < right) {
- quick_sort(a, i, right);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment