Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- typedef long long ll;
- int n;
- vector <int> a(5001,0);
- vector < vector <int> > x(5001,vector <int>(5001,0));
- vector <int> dp(5001,0);
- vector <int> first(5001,10e6);
- vector <int> last(5001,0);
- int cur(int left, int right)
- {
- int pff(0);
- for (int i=left;i<=right;i++)
- {
- for (int j=i;j<=right;j++)
- {
- if (a[i]!=a[j])
- {
- pff=max(pff,x[i][j]);
- //cout << x[i][j] << " ";
- }
- else{
- pff=max(pff,a[i]);
- // cout << i << " ";
- }
- //cout << a[i] << " ";
- }
- }
- return pff;
- }
- int main()
- {
- ios::sync_with_stdio(0); cin.tie(0) ; cout.tie(0);
- #ifdef _DEBUG
- cout << setprecision(6) << fixed ;
- #endif
- for (int i=1;i<=5000;i++)
- {
- for (int j=i+1;j<=5000;j++)
- {
- ll pf=i^j;
- x[i][j]=pf;
- x[j][i]=pf;
- }
- }
- cin >> n;
- for (int i=1;i<=n;i++)
- {
- cin >> a[i];
- first[a[i]]=min(first[a[i]],i);
- last[a[i]]=max(last[a[i]],i);
- }
- //cout << cur (1 , 2);
- for (int i=1;i<=n;i++)
- {
- for (int j=i;j>=1;j--)
- {
- bool cheker=true;
- for (int z=j;z<=i and cheker==true;z++)
- {
- if (first[a[z]]>=j and last[a[z]]<=i)
- {
- }
- else{
- cheker=false;
- }
- //cout << cheker << " " << i << " ";
- ; }
- if (cheker)
- {
- dp[i]=max(dp[i],dp[j-1]+cur(i , j));
- //cout << cur(i , j) << "\n";
- }
- else{
- dp[i]=max(dp[i],dp[i-1]);
- }
- if (i==j and first[a[i]]==i and last[a[i]]==i)
- {
- dp[i]=max(dp[i] , a[i]);
- }
- //cout << cur(i , j) << "\n" ;
- }
- }
- //cout << dp[n] << " ";
- /*for (int i=1;i<=n;i++)
- {
- cout << first[i] <<" " <<last[i] << "\n";
- }
- */
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement