(dp) Codeforces Round #416 (Div. 2)

Vladik often travels by trains. He remembered some of his trips especially well and I would like to tell you about one of these trips:

Vladik is at initial train station, and now n people (including Vladik) want to get on the train. They are already lined up in some order, and for each of them the city code ai is known (the code of the city in which they are going to).

Train chief selects some number of disjoint segments of the original sequence of people (covering entire sequence by segments is not necessary). People who are in the same segment will be in the same train carriage. The segments are selected in such way that if at least one person travels to the city x, then all people who are going to city x should be in the same railway carriage. This means that they can’t belong to different segments. Note, that all people who travel to the city x, either go to it and in the same railway carriage, or do not go anywhere at all.

Comfort of a train trip with people on segment from position l to position r is equal to XOR of all distinct codes of cities for people on the segment from position l to position r. XOR operation also known as exclusive OR.

Total comfort of a train trip is equal to sum of comfort for each segment.

Help Vladik to know maximal possible total comfort.

Input

First line contains single integer n (1?≤?n?≤?5000) — number of people.

Second line contains n space-separated integers a1,?a2,?...,?an (0?≤?ai?≤?5000), where ai denotes code of the city to which i-th person is going.

Output

The output should contain a single integer — maximal possible total comfort.

Example

Input

64 4 2 5 2 3

Output

14

Input

95 1 3 1 5 2 4 2 5

Output

9

Note

In the first test case best partition into segments is: [4,?4] [2,?5,?2] [3], answer is calculated as follows: 4?+?(2 xor 5)?+?3?=?4?+?7?+?3?=?14

In the second test case best partition into segments is: 5 1 [3] 1 5 [2,?4,?2] 5, answer calculated as follows: 3?+?(2 xor 4)?=?3?+?6?=?9.

用dp[i]表示只用[1,i]区间的数按题目要求能构成的最大值。

对于i+1,若不取a[i+1] 则dp[i+1]=dp[i]

若取i+1,从i+1向左看,不断更新“当前区间中的数第一次出现的位置中的最小值”,并且如果某一位置的数最后一次出现的下标大于i+1就break。每当在“当前区间中的数第一次出现的位置中的最小值”左侧时更新dp[i+1]的值。

 1 #include <iostream>
 2 #include <string>
 3 #include <algorithm>
 4 #include <cstring>
 5 #include <cstdio>
 6 #include <cmath>
 7 #include <queue>
 8 #include <set>
 9 #include <map>
10 #include <list>
11 #include <vector>
12 #include <stack>
13 #define mp make_pair
14 #define MIN(a,b) (a>b?b:a)
15 #define rank rankk
16 //#define MAX(a,b) (a>b?a:b)
17 typedef long long ll;
18 typedef unsigned long long ull;
19 const int MAX=5e3+5;
20 const int INF=2147483647;
21 const int B=1024;//桶的大小
22 const double M=4e18;
23 using namespace std;
24 const int MOD=1e9+7;
25 typedef pair<int,int> pii;
26 int st[MAX],en[MAX],dp[MAX],num[MAX];
27 bool vi[MAX];
28 int n,tem,re,qian;
29 int main()
30 {
31     scanf("%d",&n);
32     for(int i=1;i<=n;i++)
33     {
34         scanf("%d",&tem);
35         num[i]=tem;
36         if(!st[tem])
37             st[tem]=i;
38         en[tem]=i;
39     }
40     for(int i=1;i<=n;i++)
41     {
42         tem=num[i];
43         dp[i]=dp[i-1];
44         re=0;
45         memset(vi,false,sizeof(vi));
46         int j;
47         qian=st[num[i]];
48         for(j=i;j>=1;j--)
49         {
50             if(en[num[j]]>i)
51                 break;
52             else if(!vi[num[j]])
53             {
54                 qian=min(qian,st[num[j]]);
55                 vi[num[j]]=true;
56                 re^=num[j];
57             }
58             if(j<=qian)
59                 dp[i]=max(dp[i],dp[j-1]+re);
60         }
61     }
62     printf("%d\n",dp[n]);
63 }
时间: 2024-08-26 16:13:49

(dp) Codeforces Round #416 (Div. 2)的相关文章

Codeforces Round #416 (Div. 2) C. Vladik and Memorable Trip

题目链接:Codeforces Round #416 (Div. 2) C. Vladik and Memorable Trip 题意: 给你n个数,现在让你选一些区间出来,对于每个区间中的每一种数,全部都要出现在这个区间. 每个区间的价值为该区间不同的数的异或值,现在问你这n个数最大的价值是多少. 题解: 比赛的时间直接就想到了做法,不过在选取合法区间的时候,细节上出了点小问题. 然后一直wa到怀疑人生.太菜了. 首先,先将合法的区间选取出来. 对于这些区间,按照左端点排序, 然后对于选出来的

DP Codeforces Round #303 (Div. 2) C. Woodcutters

题目传送门 1 /* 2 题意:每棵树给出坐标和高度,可以往左右倒,也可以不倒 3 问最多能砍到多少棵树 4 DP:dp[i][0/1/2] 表示到了第i棵树时,它倒左或右或不动能倒多少棵树 5 分情况讨论,若符合就取最大值更新,线性dp,自己做出来了:) 6 */ 7 #include <cstdio> 8 #include <algorithm> 9 #include <cstring> 10 #include <cmath> 11 #include &

DP Codeforces Round #FF (Div. 1) A. DZY Loves Sequences

题目传送门 /* DP:先用l,r数组记录前缀后缀上升长度,最大值会在三种情况中产生: 1. a[i-1] + 1 < a[i+1],可以改a[i],那么值为l[i-1] + r[i+1] + 1 2. l[i-1] + 1 3. r[i+1] + 1 //修改a[i] */ #include <cstdio> #include <algorithm> #include <cstring> using namespace std; const int MAXN =

递推DP Codeforces Round #260 (Div. 1) A. Boredom

题目传送门 1 /* 2 DP:从1到最大值,dp[i][1/0] 选或不选,递推更新最大值 3 */ 4 #include <cstdio> 5 #include <algorithm> 6 #include <cmath> 7 #include <cstring> 8 using namespace std; 9 10 typedef long long ll; 11 const int MAXN = 1e5 + 10; 12 const int INF

树形DP Codeforces Round #135 (Div. 2) D. Choosing Capital for Treeland

题目传送门 1 /* 2 题意:求一个点为根节点,使得到其他所有点的距离最短,是有向边,反向的距离+1 3 树形DP:首先假设1为根节点,自下而上计算dp[1](根节点到其他点的距离),然后再从1开始,自上而下计算dp[v], 4 此时可以从上个节点的信息递推出来 5 */ 6 #include <cstdio> 7 #include <cstring> 8 #include <cmath> 9 #include <vector> 10 using name

DP Codeforces Round #260 (Div. 1) A. Boredom

题目传送门 1 /* 2 题意:选择a[k]然后a[k]-1和a[k]+1的全部删除,得到点数a[k],问最大点数 3 DP:状态转移方程:dp[i] = max (dp[i-1], dp[i-2] + (ll) i * cnt[i]); 4 只和x-1,x-2有关,和顺序无关,x-1不取,x-2取那么累加相同的值,ans = dp[mx] 5 */ 6 #include <cstdio> 7 #include <algorithm> 8 #include <cstring&

数学+DP Codeforces Round #304 (Div. 2) D. Soldier and Number Game

题目传送门 1 /* 2 题意:这题就是求b+1到a的因子个数和. 3 数学+DP:a[i]保存i的最小因子,dp[i] = dp[i/a[i]] +1;再来一个前缀和 4 */ 5 /************************************************ 6 Author :Running_Time 7 Created Time :2015-8-1 14:08:34 8 File Name :B.cpp 9 ******************************

树形dp - Codeforces Round #322 (Div. 2) F Zublicanes and Mumocrates

Zublicanes and Mumocrates Problem's Link Mean: 给定一个无向图,需要把这个图分成两部分,使得两部分中边数为1的结点数量相等,最少需要去掉多少条边. analyse: 树形dp. dp[cur][i][j]:当cur为i集合的时候 有j个和i同集合的方案 状态转移方程为: dp[cur][i][j+k] = min{dp[to][i^1][j=(叶子个数-d)]+dp[cur][i][k]+1,dp[to][i][j]+dp[cur][i][k]} c

Codeforces Round #416 (Div. 2) A+B

A. Vladik and Courtesy 2 seconds 256 megabytes At regular competition Vladik and Valera won a and b candies respectively. Vladik offered 1 his candy to Valera. After that Valera gave Vladik 2 his candies, so that no one thought that he was less gener