lanobezkari's blog

By lanobezkari, history, 14 months ago, In English

Problem Link

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
vector<int> dp;
map<int,int> mp;
const int mx = 1e5;
int n;

int solver(int range, int sum) {
    if(dp[range] != -1) return dp[range] = max(dp[range], sum);
    if(range > 1e5) return dp[range] = sum;
    if(range == mx) return dp[range] = sum + range * mp[range];
    return dp[range] = max(solver(range+2, sum + (range*mp[range])), solver(range+3, sum + ((range+1)*mp[range+1])));
}

int main() {
    cin>>n;
    dp.resize(1000006,-1);
    int temp;
    for(int i = 0; i<n; i++) {
        cin>>temp;
        mp[temp]++;
    }
    cout<<solver(1,0)<<endl;
    
    return 0;
}

It gives error on Testcase 9. I have no idea about what I'm doing wrong.

Idea of the code is simple. Iterate through 1 to 1e5. Take max(i, i+1).

I'd be glad if you help me. Thanks in advance.

  • Vote: I like it
  • 0
  • Vote: I do not like it

| Write comment?