http://codeforces.com/gym/100989/problem/L

L. Plus or Minus (A)
time limit per test

1.0 s

memory limit per test

256 MB

input

standard input

output

standard output

AbdelKader enjoys math. He feels very frustrated whenever he sees an incorrect equation and so he tries to make it correct as quickly as possible!

Given an equation of the form: A1 o A2 o A3 o ... o An  =  0, where o is either + or -. Your task is to help AbdelKader find the minimum number of changes to the operators + and -, such that the equation becomes correct.

You are allowed to replace any number of pluses with minuses, and any number of minuses with pluses.

Input

The first line of input contains an integer N (2 ≤ N ≤ 20), the number of terms in the equation.

The second line contains N integers separated by a plus + or a minus -, each value is between 1 and 108.

Values and operators are separated by a single space.

Output

If it is impossible to make the equation correct by replacing operators, print  - 1, otherwise print the minimum number of needed changes.

Examples
Input

Copy
7
1 + 1 - 4 - 4 - 4 - 2 - 2
Output

Copy
3
Input

Copy
3
5 + 3 - 7
Output

Copy
-1
//#include <bits/stdc++.h>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <iostream>
#include <algorithm>
#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <stdio.h>
#include <queue>
#include <stack>;
#include <map>
#include <set>
#include <ctype.h>
#include <string.h>
#include <vector>
#define ME(x , y) memset(x , y , sizeof(x))
#define SF(n) scanf("%d" , &n)
#define rep(i , n) for(int i = 0 ; i < n ; i ++)
#define INF 0x3f3f3f3f
#define mod 10
#define PI acos(-1)
using namespace std;
typedef long long ll ;
int a[];
char c[];
int n ;
int min1 = INF ;
int vis[]; void dfs(int s , int l , int ans)
{
//cout << s << " " << l << " " << ans << endl ;
if(l == n)
{
if(s == )
{
min1 = min(min1 , ans);
}
return ;
}
if(c[l] == '-')
{
dfs(s+a[l] , l+ , ans + );
dfs(s-a[l] , l+ , ans);
}
if(c[l] == '+')
{
dfs(s+a[l] , l+ , ans);
dfs(s-a[l] , l+ , ans+);
}
} int main()
{
scanf("%d" , &n);
scanf("%d " , &a[]);
for(int i = ; i < n ; i++)
{
cin >> c[i] >> a[i];
}
dfs(a[] , , );
if(min1 != INF)
cout << min1 << endl ;
else
cout << - << endl ; return ;
}

dfs(枚举)的更多相关文章

  1. POJ1288 Sly Number(高斯消元 dfs枚举)

    由于解集只为{0, 1, 2}故消元后需dfs枚举求解 #include<cstdio> #include<iostream> #include<cstdlib> ...

  2. POJ 2429 GCD & LCM Inverse (Pollard rho整数分解+dfs枚举)

    题意:给出a和b的gcd和lcm,让你求a和b.按升序输出a和b.若有多组满足条件的a和b,那么输出a+b最小的.思路:lcm=a*b/gcd   lcm/gcd=a/gcd*b/gcd 可知a/gc ...

  3. POJ 1270 Following Orders (拓扑排序,dfs枚举)

    题意:每组数据给出两行,第一行给出变量,第二行给出约束关系,每个约束包含两个变量x,y,表示x<y.    要求:当x<y时,x排在y前面.让你输出所有满足该约束的有序集. 思路:用拓扑排 ...

  4. HDU 2489 Minimal Ratio Tree(dfs枚举+最小生成树)

    想到枚举m个点,然后求最小生成树,ratio即为最小生成树的边权/总的点权.但是怎么枚举这m个点,实在不会.网上查了一下大牛们的解法,用dfs枚举,没想到dfs还有这么个作用. 参考链接:http:/ ...

  5. poj 1753 Flip Game(bfs状态压缩 或 dfs枚举)

    Description Flip game squares. One side of each piece is white and the other one is black and each p ...

  6. POJ 1753 Flip Game (DFS + 枚举)

    题目:http://poj.org/problem?id=1753 这个题在開始接触的训练计划的时候做过,当时用的是DFS遍历,其机制就是把每一个棋子翻一遍.然后顺利的过了.所以也就没有深究. 省赛前 ...

  7. HDU1045 Fire Net(DFS枚举||二分图匹配) 2016-07-24 13:23 99人阅读 评论(0) 收藏

    Fire Net Problem Description Suppose that we have a square city with straight streets. A map of a ci ...

  8. poj 2965 The Pilots Brothers&#39; refrigerator(dfs 枚举 +打印路径)

    链接:poj 2965 题意:给定一个4*4矩阵状态,代表门的16个把手.'+'代表关,'-'代表开.当16个把手都为开(即'-')时.门才干打开,问至少要几步门才干打开 改变状态规则:选定16个把手 ...

  9. The Pilots Brothers' refrigerator DFS+枚举

    Description The game “The Pilots Brothers: following the stripy elephant” has a quest where a player ...

  10. POJ 1753 Flip Game DFS枚举

    看题传送门:http://poj.org/problem?id=1753 DFS枚举的应用. 基本上是参考大神的.... 学习学习.. #include<cstdio> #include& ...

随机推荐

  1. linux局域网通过映射(双网卡)访问外网

    图示说明: 1.IP规划设置 主机名 ip地址 ip地址(第二个网卡配置的地址) 地址类别 oldboy01 192.168.10.20 空 仅可访问内网主机 oldboy02 192.168.10. ...

  2. 【LuoguP4770】[NOI2018] 你的名字

    题目链接 题意简述 给定一个串 \(S\) 多组询问 , 每次给定一个串 \(T\) 和一个 区间 \([l,r]\) 求串\(T\) 有多少个本质不同的子串 满足不是 \(S[l...r]\) 的子 ...

  3. POJ-1459-Pwoer Network(最大流Dinic, 神仙输入)

    链接: https://vjudge.net/problem/POJ-1459 题意: A power network consists of nodes (power stations, consu ...

  4. mybatis config 快速生成xml DAO

    0.加jar包 1.a.BAT java -jar E:\GZH\Mybaits\mybatis-generator-core-1.3.2\lib\mybatis-generator-core-1.3 ...

  5. mybatis时间范围查询

    <if test="excStartTime!=null and excStartTime!=''"> <![CDATA[ and DATE_FORMAT(dl. ...

  6. linux入门 一些常见命令

    chvt 切换终端 用法 1是终端 7是图形界面 sudo chvt 1 or 7 cp 复制文件/目录 -a : 通常在复制目录时使用,保存链接,文件属性并递归复制目录 -f:若文件在目标路径中则强 ...

  7. Redis之Java客户端Jedis

    导读 Redis不仅使用命令客户端来操作,而且可以使用程序客户端操作. 现在基本上主流的语言都有客户端支持,比如Java.C.C#.C++.php.Node.js.Go等. 在官方网站里列一些Java ...

  8. fengmiantu4

  9. picker组件 label组件讲解

    label组件:包住表单的组件,将里面的表单和label里的元素,紧紧的包在一起,当触发label里的元素,就相当于触发了表单组件 属性:for:类型 字符串 表单的 id 的值 picker组件:是 ...

  10. canvas介绍(画布)

    canvas(画布)主要是位图 svg(矢量图) canvas标签,必须要写的3个属性 id width height 为什么不再style中设置width和height呢? 因为这设置width和h ...