Why it is giving wrong ans?

#include
#include<bits/stdc++.h>
using namespace std;

long long int solve(vector arr,int n){
long long int freq[100000]={0};
for(long long int i=0;i<n;i++){
freq[arr[i]]+=arr[i];
}

long long int dp[10000]={0};
dp[1]=freq[1];
dp[2]=max(freq[1],freq[2]);
for(long long int i=3;i<=n;i++){
	dp[i]=max(dp[i-1],dp[i-2]+freq[i]);
}
return max(dp[n],dp[n-1]);

}

int main() {
int n;
cin >> n;
vector arr(n);
for(int i=0;i<n;i++){
cin >> arr[i];
}

cout << solve(arr,n);
return 0;

}