#include <bits/stdc++.h>
using namespace std;
const long long thaonguyen = 1e5 + 5;
long long n, a[thaonguyen], dung[thaonguyen], deodung[thaonguyen],
    max1 = -thaonguyen, maxval = -10000000;
signed main() {
  memset(a, 0, sizeof(a));
  cin >> n;
  for (long long i = 1; i <= n; i++) {
    cin >> a[thaonguyen - 1];
    maxval = max(maxval, a[thaonguyen - 1]);
    a[a[thaonguyen - 1]] += a[thaonguyen - 1];
  }
  for (long long i = 1; i <= maxval; i++) {
    dung[i] = deodung[i - 1] + a[i];
    deodung[i] = max(deodung[i - 1], dung[i - 1]);
    max1 = max(max1, max(dung[i], deodung[i]));
  }
  cout << max1;
}
