Skip to content

Instantly share code, notes, and snippets.

@sreeprasad
Created August 14, 2019 03:45
Show Gist options
  • Save sreeprasad/7de965ffbdae08db820dfdff266e3a0d to your computer and use it in GitHub Desktop.
Save sreeprasad/7de965ffbdae08db820dfdff266e3a0d to your computer and use it in GitHub Desktop.
public void findMax(int N) throws Exception {
final int maxN = 100007;
long [] freqMap = new long[maxN];
for(int i=1;i<=N;i++){
freqMap[getNextInt()]++;
}
long [] dp = new long[maxN];
dp[0] = 0;
dp[1] = freqMap[1];
for(int i=2;i<maxN;i++){
dp[i] = Math.max(dp[i-1], dp[i-2] + freqMap[i]*i);
//System.out.printf("%d: dp[%d]: %d\n",i,i, dp[i]);
}
System.out.printf("%d\n", dp[maxN-1]);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment