A11047 | Vladik and Memorable Trip
时间限制1s
内存限制256MB
通过 / 提交0/0
题目描述
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 $a_{i}$ 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.
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 $a_{i}$ 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.
输入格式
First line contains single integer $n$ ( $1<=n<=5000$ ) — number of people.
Second line contains $n$ space-separated integers $a_{1},a_{2},...,a_{n}$ ( $0<=a_{i}<=5000$ ), where $a_{i}$ denotes code of the city to which $i$ -th person is going.
Second line contains $n$ space-separated integers $a_{1},a_{2},...,a_{n}$ ( $0<=a_{i}<=5000$ ), where $a_{i}$ denotes code of the city to which $i$ -th person is going.
输出格式
The output should contain a single integer — maximal possible total comfort.
输入输出样例
输入 #1
6 4 4 2 5 2 3
输出 #1
14
输入 #2
9 5 1 3 1 5 2 4 2 5
输出 #2
9
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$ .
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$ .
C++ 编辑器
输入
输出
可保存默认模板;新题优先使用已保存模板。
当前快捷键仅展示,暂不支持修改。
- 撤销
Ctrl / ⌘ + Z - 重做
Ctrl / ⌘ + Y - 查找
Ctrl / ⌘ + F - 全选
Ctrl / ⌘ + A - 复制
Ctrl / ⌘ + C - 剪切
Ctrl / ⌘ + X - 粘贴
Ctrl / ⌘ + V - 自动排版
工具栏排版按钮 - 草稿保存
编辑时自动保存到本机
历史
提交记录
状态说明时间源码
AI
作答助手
你好,我是作答助手。可以问思路、复杂度、样例含义或代码报错原因;不会直接给出完整 AC 代码。
确定要清空代码吗?
提交通过
评测结果:Accepted