fork download
  1. #include<bits/stdc++.h>
  2. #define ll long long
  3. #define N_max 1e6
  4. using namespace std ;
  5. ll n , cnt , ma ;
  6. ll a[int(1e6)] , f[int(1e6)] , dau[int(1e6)];
  7. int main(){
  8. ios_base::sync_with_stdio(0);
  9. cin.tie(0) ; cout.tie(0);
  10. cin >> n ;
  11.  
  12. for ( ll i = 1 ; i <= n ; i++ ){
  13. cin >> a[i] ;
  14. dau[a[i]] ++ ;
  15. ma = max(ma,a[i]) ;
  16. }
  17.  
  18. f[1] = dau[1] ;
  19. for ( ll i = 1 ; i <= ma ; i++ )
  20. f[i] = max ( f[i-1] , f[i-2] + dau[i]*i ) ;
  21.  
  22. cout << f[ma] ;
  23.  
  24. }
  25.  
Success #stdin #stdout 0s 5640KB
stdin
Standard input is empty
stdout
Standard output is empty