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 ;
}

最新文章

  1. Java程序设计之消费者和生产者
  2. Python自动化测试 (九)urllib2 发送HTTP Request
  3. CAD二次开发
  4. 【JAVA线程间通信技术】
  5. 生日蛋糕—dfs
  6. Xcode工程使用CocoaPods管理第三方库新建工程时出现错误
  7. Easyui使用记录
  8. Python入门(三,初级)
  9. 简单Spinner
  10. Notepad++去除代码行号的几种方法
  11. linux上备份Oracle时EXP-00091的错误解决方法
  12. linux命令学习笔记
  13. nodejs在服务器上运行
  14. VR问题无关方向,VR全景为您领航,全景智慧城市已势不可当
  15. Android 类似duplicate entry: android/support/v4/internal/view/SupportSubMenu.class问题解决办法汇总
  16. java里程碑之泛型--类型通配符
  17. Django 项目中添加静态文件夹
  18. bootstrap-typeahead 自动补全简单的使用教程
  19. python 微信跳一跳辅助 复现
  20. HNOI 2017

热门文章

  1. ivew 封装删除 对话框
  2. Jmeter性能测试结果分析:响应时间为什么是下降的趋势?
  3. LOJ#2330 榕树之心 树形dp
  4. Tymeleaf模板引擎背景图片路径书写方式
  5. 3分钟教会你把封装的js公共方法挂载在vue实例原型上
  6. Serverless Kubernetes入门:对kubernetes做减法
  7. java——逻辑运算符与(&amp;和&amp;&amp;)或(|和||)
  8. 状态管理工具对比vuex、redux、flux
  9. 贪心整理&amp;一本通1431:钓鱼题解
  10. linux 多进程并发服务__关于子进程回收的方法