fork(4) download
  1. #include <iostream>
  2. #include <stdio.h>
  3. #include <array>
  4. #include <algorithm>
  5. using namespace std;
  6. #define MAX 100
  7.  
  8. #define gc getchar_unlocked
  9. inline int scan(){register int n=0,c=gc();while(c<'0'||c>'9')c=gc();while(c<='9'&&c>='0')n=(n<<1)+(n<<3)+c-'0',c=gc();return n;}
  10. int getCount(int n[],int len,int c)
  11. {
  12. int i=0;
  13. if(n[i+1]!=9999){
  14. n[i+1]+=n[i];
  15. n[i]=9999;
  16. c+=n[i+1];
  17. sort(n,n+len);
  18. }
  19. if(n[i+1]==9999)return c;
  20. if(len>0) getCount(n,len-1,c);
  21. }
  22.  
  23. int main()
  24. {
  25. int len=scan();
  26. int j = len;
  27. int n[MAX];
  28. int sum=0;
  29. int i = 0;
  30. while(j--){
  31. n[i++]=scan();
  32. }
  33. j=len;
  34.  
  35. sort(n,n+len);
  36. sum=getCount(n,len,sum);
  37. cout<<"Sum is="<<sum;
  38. return 0;
  39. }
  40. //STDIN : 4 2 4 3 6
Success #stdin #stdout 0s 3304KB
stdin
4 2 4 3 6
stdout
Sum is=29