fork(2) download
  1. #include <iostream>
  2. #include <algorithm>
  3. #include <string.h>
  4. #define ll long long
  5. using namespace std;
  6. ll a[100007];
  7. ll b[100007];
  8. bool c[100007];
  9. bool d[100007];
  10. int main()
  11. {
  12. for(int i=0;i<=100006;i++)
  13. {
  14. a[i]=0;
  15. b[i]=0;
  16. c[i]=0;
  17. d[i]=0;
  18. }
  19. ll n;
  20. cin>>n;
  21. for(int i=0;i<n;i++)
  22. {
  23. cin>>a[i];
  24. b[a[i]]++;
  25. }
  26. ll temp=0;
  27. for(int i=1;i<=100002;i+=2)
  28. {
  29. temp += (b[i]*i);
  30. c[i]=1;
  31. }
  32. for(int i=2;i<=100002;i+=2)
  33. {
  34. if(b[i]*i > (b[i-1]*(i-1) + b[i+1]*(i+1)))
  35. {
  36. temp += (b[i]*i);
  37. if(c[i-1]==1)
  38. {
  39. temp -= (b[i-1]*(i-1));
  40. c[i-1]=0;
  41. }
  42. if(c[i+1]==1)
  43. {
  44. c[i+1]=0;
  45. temp -= (b[i+1]*(i+1));
  46. }
  47.  
  48. }
  49. }
  50.  
  51.  
  52.  
  53.  
  54.  
  55. ll temp2=0;
  56.  
  57. for(int i=2;i<=100002;i+=2)
  58. {
  59. temp2 += (b[i]*i);
  60. d[i]=1;
  61. }
  62. for(int i=1;i<=100002;i+=2)
  63. {
  64. if(b[i]*i > (b[i-1]*(i-1) + b[i+1]*(i+1)))
  65. {
  66. temp2 += (b[i]*i);
  67. if(d[i-1]==1)
  68. {
  69. temp2 -= (b[i-1]*(i-1));
  70. d[i-1]=0;
  71. }
  72. if(d[i+1]==1)
  73. {
  74. d[i+1]=0;
  75. temp2 -= (b[i+1]*(i+1));
  76. }
  77. }
  78. }
  79.  
  80. cout<<((temp>temp2)?temp:temp2);
  81.  
  82. }
Success #stdin #stdout 0s 5056KB
stdin
Standard input is empty
stdout
Standard output is empty