fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. #define sp " "
  5. #define el '\n'
  6. #define ll long
  7. void Solution()
  8. {
  9. unsigned ll n, ev = 0, od = 0;
  10. cin >> n;
  11. vector<unsigned ll> v(n), o,e;
  12. for(ll i = 0; i < n; i++){
  13. cin >> v[i];
  14. if(v[i]%2) {
  15. od++;
  16. o.push_back(v[i]);
  17. }
  18. else{
  19. ev++;
  20. e.push_back(v[i]);
  21. }
  22. }
  23. sort(e.rbegin(),e.rend());
  24. sort(o.rbegin(),o.rend());
  25.  
  26. vector<unsigned ll> pref(ev),ans;
  27. if(ev){
  28. pref[0] = e[0];
  29. for(ll i = 1; i < ev; i++){
  30. pref[i] = pref[i-1] + e[i];
  31. }
  32. }
  33. if(od == 0) {
  34. for(int i = 0; i < n; i++){
  35. cout << 0 << sp;
  36. }
  37. cout << endl;
  38. return;
  39. }
  40. if(ev == 0){
  41. for(int i = 0; i < n; i++){
  42. if(i%2 == 0) cout << o[0] << sp;
  43. else cout << 0 << sp;
  44. }
  45. cout << endl;
  46. return;
  47. }
  48. ans.push_back(o[0]);
  49. for(ll i = 1; i < n; i++){
  50. if(i <= ev)
  51. ans.push_back(o[0]+pref[i-1]);
  52. else{
  53. if(i == n-1 && od%2 == 0)
  54. ans.push_back(0);
  55. else{
  56. if(((i+1) - (ev+1)) %2 == 0)
  57. ans.push_back(o[0] + pref[ev-1]);
  58. else {
  59. if(ev > 1)
  60. ans.push_back(o[0] + pref[ev-2]);
  61. else ans.push_back(o[0]);
  62. }
  63. }
  64. }
  65. }
  66. for(auto u : ans){
  67. cout << u << sp;
  68. }
  69. cout << endl;
  70. }
  71. int main()
  72. {
  73. ios::sync_with_stdio(0);
  74. cin.tie(0); cout.tie(0);
  75. int t;
  76. cin >> t;
  77. for(int tc = 1; tc <= t; tc++)
  78. Solution();
  79. return 0;
  80. }
Success #stdin #stdout 0.06s 5316KB
stdin
Standard input is empty
stdout