Advertisement
Dang_Quan_10_Tin

MAKESTR HSGVinh Phuc L9 2015-2016

Jan 15th, 2022
996
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.05 KB | None | 0 0
  1. #define task "MAKESTR"
  2.  
  3. #include <iostream>
  4. #include <cstdio>
  5. #include <algorithm>
  6.  
  7. using namespace std;
  8.  
  9. using ll = long long;
  10. using ld = long double;
  11.  
  12. constexpr int N = 1e5 + 5;
  13. int n, a[N], b[N];
  14.  
  15. void Read()
  16. {
  17.     cin >> n;
  18.  
  19.     for (int i = 1; i <= n; ++i)
  20.         cin >> a[i];
  21. }
  22.  
  23. void Solve()
  24. {
  25.     sort(a + 1, a + n + 1);
  26.  
  27.     for (int i = n; i; --i)
  28.     {
  29.         if (a[i] == 0)
  30.         {
  31.             for (int j = 1; j <= i; ++j)
  32.                 b[j] = 0;
  33.             break;
  34.         }
  35.         if (i == 1)
  36.         {
  37.             b[i] = a[i];
  38.             break;
  39.         }
  40.  
  41.         b[i] = a[i];
  42.  
  43.         for (int j = 1; j < i; ++j)
  44.             a[j] = min(a[j], a[i] - 1);
  45.     }
  46.  
  47.     ll ans(0);
  48.  
  49.     for (int i = 1; i <= n; ++i)
  50.         ans += b[i];
  51.  
  52.     cout << ans;
  53. }
  54.  
  55. int32_t main()
  56. {
  57.     ios::sync_with_stdio(0);
  58.     cin.tie(0);
  59.     cout.tie(0);
  60.     if (fopen(task ".INP", "r"))
  61.     {
  62.         freopen(task ".INP", "r", stdin);
  63.         freopen(task ".OUT", "w", stdout);
  64.     }
  65.  
  66.     Read();
  67.     Solve();
  68. }
  69.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement