time limit per test
1 second
memory limit per test
512 megabytes
input
standard input
output
standard output
A group of nn dancers rehearses a performance for the closing ceremony. The dancers are arranged in a row, they've studied their dancing moves and can't change positions. For some of them, a white dancing suit is already bought, for some of them — a black one, and for the rest the suit will be bought in the future.
On the day when the suits were to be bought, the director was told that the participants of the olympiad will be happy if the colors of the suits on the scene will form a palindrome. A palindrome is a sequence that is the same when read from left to right and when read from right to left. The director liked the idea, and she wants to buy suits so that the color of the leftmost dancer's suit is the same as the color of the rightmost dancer's suit, the 2nd left is the same as 2nd right, and so on.
The director knows how many burls it costs to buy a white suit, and how many burls to buy a black suit. You need to find out whether it is possible to buy suits to form a palindrome, and if it's possible, what's the minimal cost of doing so. Remember that dancers can not change positions, and due to bureaucratic reasons it is not allowed to buy new suits for the dancers who already have suits, even if it reduces the overall spending.
Input
The first line contains three integers nn, aa, and bb (1≤n≤201≤n≤20, 1≤a,b≤1001≤a,b≤100) — the number of dancers, the cost of a white suit, and the cost of a black suit.
The next line contains nn numbers cici, ii-th of which denotes the color of the suit of the ii-th dancer. Number 00 denotes the white color, 11 — the black color, and 22 denotes that a suit for this dancer is still to be bought.
Output
If it is not possible to form a palindrome without swapping dancers and buying new suits for those who have one, then output -1. Otherwise, output the minimal price to get the desired visual effect.
Examples
input
Copy
5 100 1 0 1 2 1 2
output
Copy
101
input
Copy
3 10 12 1 2 0
output
Copy
-1
input
Copy
3 12 1 0 1 0
output
Copy
0
Note
In the first sample, the cheapest way to obtain palindromic colors is to buy a black suit for the third from left dancer and a white suit for the rightmost dancer.
In the second sample, the leftmost dancer's suit already differs from the rightmost dancer's suit so there is no way to obtain the desired coloring.
In the third sample, all suits are already bought and their colors form a palindrome.
题意翻译
题目大意
给你一个序列,里面的元素只能是0,10,1或22,22可以通过代价换成11或者00,问形成回文串的最小代价
输入格式
第一行3个整数n,a,bn,a,b,分别代表了序列长度,换成00的代价,换成11的代价
第二行nn个整数,表示序列
输出格式
如果能构成回文串,输出最小代价,否则输出-1−1
#include <iostream>
#include<stdio.h>
#include<math.h>
using namespace std;
int s[1010];
int main()
{
int n,a,b;
scanf("%d%d%d",&n,&a,&b);
int i,j;
for(i=0; i<n; i++)
scanf("%d",&s[i]);
int ans=0;
int flas=0;
int l=min(a,b);
for(i=0, j=n-1; j>i; j--,i++)
{
if((s[i]==1&&s[j]==0)||(s[j]==1&&s[i]==0))
{
flas=1;
break;
}
if(s[i]==2&&s[j]==2)
ans+=l*2;
else if(s[i]==2)
{
if(s[j]==1)
ans+=b;
else ans+=a;
}
else if(s[j]==2)
{
if(s[i]==1)
ans+=b;
else ans+=a;
}
}
if(n%2!=0&&s[n/2]==2)ans+=min(a,b);
if(flas)printf("-1\n");
else printf("%d\n",ans);
return 0;
}