fork download
  1. /* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 4; tab-width: 4 -*- */
  2. /*
  3.  * main.c
  4.  * Copyright (C) 2013 Akhil <akhil@akhil-VPCEA33EN>
  5.  *
  6.  * Hello is free software: you can redistribute it and/or modify it
  7.  * under the terms of the GNU General Public License as published by the
  8.  * Free Software Foundation, either version 3 of the License, or
  9.  * (at your option) any later version.
  10.  *
  11.  * Hello is distributed in the hope that it will be useful, but
  12.  * WITHOUT ANY WARRANTY; without even the implied warranty of
  13.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  14.  * See the GNU General Public License for more details.
  15.  *
  16.  * You should have received a copy of the GNU General Public License along
  17.  * with this program. If not, see <http://w...content-available-to-author-only...u.org/licenses/>.
  18.  */
  19.  
  20. #include <stdio.h>
  21. int main()
  22. {
  23. int test=0, test_loop=0, loop=0; //number of test cases
  24. int n=0, k=0,*arr=NULL,*sum_dad=NULL,*sum_son = NULL;
  25. int i=0, j=0, tmp=0/*, sum_dad=0, sum_son=0*/;//loop variables
  26. // printf("\nNumber of test cases");
  27. scanf("%d",&test);
  28. sum_dad= (int*)malloc(test*sizeof(int));
  29. sum_son= (int*)malloc(test*sizeof(int));
  30. // memset(sum_dad,0,(test*sizeof(int));
  31. // memset(sum_son,0,(test*sizeof(int));
  32. for(loop=0;loop<test;loop++)
  33. {
  34. //printf("\nEnter N,K");
  35. scanf("%d %d",&n,&k);
  36. arr= (int*)malloc(n*sizeof(int));
  37.  
  38. sum_son[loop]=0;
  39. sum_dad[loop]=0;
  40.  
  41.  
  42. for(i=0;i<n;i++)
  43. {
  44. scanf("%d",&arr[i]);//enter value for all the n entries
  45. }
  46.  
  47. //printf("Numbers :: %d , %d",arr[0],arr[1]);
  48.  
  49. //now sort the array
  50. /**************sorting begins*****************************/
  51. for(i=0;i<n;i++)
  52. {
  53. for(j=1;j<n-i;j++)
  54. {
  55. if(arr[j-1]>arr[j])
  56. {
  57. tmp=arr[j];
  58. arr[j]=arr[j-1];
  59. arr[j-1]=tmp;
  60. }
  61. }
  62. }
  63. /*****************sorting ends******************************/
  64. if(k>(n-k)) k=n-k;
  65.  
  66. for(i=0;i<k;i++)
  67. {
  68. //add the smallest k elements
  69. sum_son[loop]=sum_son[loop]+arr[i];
  70. }
  71. for(i=k;i<n;i++)
  72. {
  73. sum_dad[loop]=sum_dad[loop]+arr[i];
  74. }
  75. free(arr);
  76. arr=NULL;
  77. //printf("differnce of weights = %d",(sum_dad[loop]-sum_son[loop]));
  78. } /*test loop ends here*/
  79.  
  80. for(loop=0;loop<test;loop++)
  81. {
  82. // printf("differnce of weights = %d",(sum_dad[loop]-sum_son[loop]));
  83. printf("%d\n",(sum_dad[loop]-sum_son[loop]));
  84. }
  85.  
  86. free(sum_son);
  87. free(sum_dad);
  88. return 0;
  89.  
  90. }
Success #stdin #stdout 0s 1968KB
stdin
2
5 2
8 4 5 2 10
8 3
1 1 1 1 1 1 1 1
stdout
17
2