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
7
1 + 1 - 4 - 4 - 4 - 2 - 2
Output
3
Input
3
5 + 3 - 7
Output
-1

题目翻译:一串数字改变其的符号,让sum为0,输出最小的改变次数,不存在则输出-1

运用算法DFS

ac代码:

#include<iostream>
using namespace std;
int a[25],n,ans;
void dfs(int index,int sum,int c)
{
  if (index==n+1){
    if (sum==0){
      ans=ans<c?ans:c;
    }
  return ;
  }
  int j;
  for (j=0;j<2;j++){      //只存在 + 或者是 - 两种情况
    if (j==0){
      if (a[index]<0)
        dfs(index+1,sum-a[index],c+1);
    else
      dfs(index+1,sum+a[index],c);
    }
    else if (a[index]<0)
      dfs(index+1,sum+a[index],c);
    else
      dfs(index+1,sum-a[index],c+1);
  }
return ;

}
int main()
{
  //scanf("%d",&n);
  char op;
  int i,j;
  ans=99999999;
  cin>>n;
  for (i=1;i<=n;i++){
    if (i==1)
      scanf("%d",&a[i]);
    else{
      scanf(" %c %d",&op,&a[i]);
      if (op=='-')
        a[i]=-a[i];
     }
  }
  dfs(2,a[1],0);
   if (ans==99999999)
    cout<<"-1\n";
  else
    cout<<ans<<endl;
  return 0;
}

dfs Gym - 100989L的更多相关文章

  1. Gym 100989L (DFS)

    AbdelKader enjoys math. He feels very frustrated whenever he sees an incorrect equation and so he tr ...

  2. DFS Gym 100553J Jokewithpermutation

    题目传送门 /* 题意:将字符串分割成一个全排列 DFS:搜索主要在一位数和两位数的处理,用d1, d2记录个数,在不饱和的情况下,两种都试一下 DFS还是写不来,难道是在家里懒? */ #inclu ...

  3. Gym - 100989L

    After the data structures exam, students lined up in the cafeteria to have a drink and chat about ho ...

  4. ACM: Gym 100935G Board Game - DFS暴力搜索

    Board Game Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u  Gym 100 ...

  5. Gym 100463D Evil DFS

    Evil Time Limit: 5 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100463/attachments Descri ...

  6. codeforces Gym 100187J J. Deck Shuffling dfs

    J. Deck Shuffling Time Limit: 2   Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100187/pro ...

  7. CodeForces Gym 100500A A. Poetry Challenge DFS

    Problem A. Poetry Challenge Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/10 ...

  8. Codeforces Gym 100463D Evil DFS

    Evil Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100463/attachments Descr ...

  9. Gym 100952E&&2015 HIAST Collegiate Programming Contest E. Arrange Teams【DFS+剪枝】

    E. Arrange Teams time limit per test:2 seconds memory limit per test:64 megabytes input:standard inp ...

随机推荐

  1. no.1 github

    正是Github,让社会化编程成为现实. github是一个基于git的代码托管平台,付费用户可以建私人仓库,我们一般的免费用户只能使用公共仓库,也就是代码要公开.它是由Chris Wanstrath ...

  2. Xpath定位_1:子找父以及contains的用法

    先上xml代码,如下图,在写自动化脚本时,需要定位到数字为10334的td元素.td元素的父元素.父的父元素以及属性值都一样:只有同胞元素的元素值不同.以此可以通过先定位到同胞元素,在找到父元素下的期 ...

  3. chpasswd

    功能说明:从标准输入中读取一定格式的用户名.密码来批量更新用户的密码,其格式为 “用户名:密码”. 参数选项:-e 默认格式是明文密码,使用-e参数则需要加密的密码.

  4. Linux下apt-get的软件一般安装路径

    apt-get安装目录和安装路径:apt-get 下载后,软件所在路径是:/var/cache/apt/archivesubuntu 默认的PATH为PATH=/home/brightman/bin: ...

  5. 模拟栈的回溯,完全二叉树搜索,(ZOJ1004)

    题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1004 解题报告: ①方法:完全二叉树的搜索方式,回溯法. ②代码 ...

  6. VK Cup 2012 Round 1 D. Distance in Tree (树形dp)

    题目:http://codeforces.com/problemset/problem/161/D 题意:给你一棵树,问你两点之间的距离正好等于k的有多少个 思路:这个题目的内存限制首先大一倍,他有5 ...

  7. spring 跨域 CORS (Cross Origin Resources Share) 跨域

    Spring提供了三种方式跨域 1.CorsFilter 过滤器 2.<mvc:cors> Bean(全局,推荐使用) 3.@CrossOrigin注解 以上三种方式本质都是用来配置Cor ...

  8. sqlite配置下载安装教程

    安装教程 第一步: 首先去官网下载:https://www.sqlite.org/download.html 或直接下载:https://github.com/weibanggang/sqlite 下 ...

  9. Android学习笔记_44_apk安装、反编译及防治反编译

    一.APK安装 1.首先需要AndroidManifest.xml中加入安装程序权限: <!-- 安装程序权限 --> <uses-permission android:name=& ...

  10. Android学习笔记_12_网络通信之从web获取资源数据到Android

    从web获取图片信息,并显示到android的imageView控件. 一.添加网络访问权限. <uses-permission android:name="android.permi ...