A.Regular bracket sequence

A string is called bracket sequence if it does not contain any characters other than "(" and ")". A bracket sequence is called regular if it it is possible to obtain correct arithmetic expression by inserting characters "+" and "1" into this sequence. For example, "", "(())" and "()()" are regular bracket sequences; "))" and ")((" are bracket sequences (but not regular ones), and "(a)" and "(1)+(1)" are not bracket sequences at all.

You have a number of strings; each string is a bracket sequence of length 22. So, overall you have cnt1cnt1 strings "((", cnt2cnt2 strings "()", cnt3cnt3 strings ")(" and cnt4cnt4strings "))". You want to write all these strings in some order, one after another; after that, you will get a long bracket sequence of length 2(cnt1+cnt2+cnt3+cnt4)2(cnt1+cnt2+cnt3+cnt4). You wonder: is it possible to choose some order of the strings you have such that you will get a regular bracket sequence? Note that you may not remove any characters or strings, and you may not add anything either.

Input

The input consists of four lines, ii-th of them contains one integer cnticnti (0≤cnti≤1090≤cnti≤109).

Output

Print one integer: 11 if it is possible to form a regular bracket sequence by choosing the correct order of the given strings, 00 otherwise.

Examples

Input
3
1
4
3
Output
1
Input
0
0
0
0
Output
1
Input
1
2
3
4
Output
0

In the first example it is possible to construct a string "(())()(()((()()()())))", which is a regular bracket sequence.

In the second example it is possible to construct a string "", which is a regular bracket sequence.


题解:水题。。。

参考代码:

 #include<bits/stdc++.h>
using namespace std;
#define clr(a,v) memset(a,v,sizeof(a))
#define PI acos(-1.0)
#define PII pair<int,int>
#define pb push_back
#define fi first
#define se second
typedef long long ll;
const int INF=0x3f3f3f3f;
int n1,n2,n3,n4;
int main()
{
cin>>n1>>n2>>n3>>n4;
if(n1!=n4) puts("");
else
{
if(n1==)
{
if(n3!=) puts("");
else puts("");
}
else puts("");
} return ;
}

B.Discount

You came to a local shop and want to buy some chocolate bars. There are nn bars in the shop, ii-th of them costs aiai coins (and you want to buy all of them).

You have mm different coupons that allow you to buy chocolate bars. ii-th coupon allows you to buy qiqi chocolate bars while you have to pay only for the qi−1qi−1 most expensive ones (so, the cheapest bar of those qiqi bars is for free).

You can use only one coupon; if you use coupon ii, you have to choose qiqi bars and buy them using the coupon, and buy all the remaining n−qin−qi bars without any discounts.

To decide which coupon to choose, you want to know what will be the minimum total amount of money you have to pay if you use one of the coupons optimally.

Input

The first line contains one integer nn (2≤n≤3⋅1052≤n≤3⋅105) — the number of chocolate bars in the shop.

The second line contains nn integers a1a1, a2a2, ..., anan (1≤ai≤1091≤ai≤109), where aiai is the cost of ii-th chocolate bar.

The third line contains one integer mm (1≤m≤n−11≤m≤n−1) — the number of coupons you have.

The fourth line contains mm integers q1q1, q2q2, ..., qmqm (2≤qi≤n2≤qi≤n), where qiqi is the number of chocolate bars you have to buy using ii-th coupon so that the least expensive of them will be for free. All values of qiqi are pairwise distinct.

Output

Print mm integers, ii-th of them should be the minimum amount of money you have to pay if you buy qiqi bars with ii-th coupon, and all the remaining bars one by one for their full price.

Example

Input
7
7 1 3 1 4 10 8
2
3 4
Output
27
30

Consider the first example.

If we use the first coupon, we may choose chocolate bars having indices 11, 66 and 77, and we pay 1818 coins for them and 99 coins for all other bars.

If we use the second coupon, we may choose chocolate bars having indices 11, 55, 66 and 77, and we pay 2525 coins for them and 55 coins for all other bars.

参考代码:

 #include<bits/stdc++.h>
using namespace std;
#define clr(a,v) memset(a,v,sizeof(a))
#define PI acos(-1.0)
#define PII pair<int,int>
#define pb push_back
#define fi first
#define se second
typedef long long ll;
const int INF=0x3f3f3f3f;
const int maxn=3e5+;
int n,m,a[maxn],q;
ll sum; int main()
{
scanf("%d",&n);
for(int i=;i<=n;++i) scanf("%d",&a[i]),sum+=a[i];
scanf("%d",&m);
sort(a+,a++n);
for(int i=;i<=m;++i)
{
scanf("%d",&q);
ll ans=sum-a[n-q+];
printf("%lld\n",ans);
} return ;
}

C.Painting the fence

You have a long fence which consists of nn sections. Unfortunately, it is not painted, so you decided to hire qq painters to paint it. ii-th painter will paint all sections xx such that li≤x≤rili≤x≤ri.

Unfortunately, you are on a tight budget, so you may hire only q−2q−2 painters. Obviously, only painters you hire will do their work.

You want to maximize the number of painted sections if you choose q−2q−2 painters optimally. A section is considered painted if at least one painter paints it.

Input

The first line contains two integers nn and qq (3≤n,q≤50003≤n,q≤5000) — the number of sections and the number of painters availible for hire, respectively.

Then qq lines follow, each describing one of the painters: ii-th line contains two integers lili and riri (1≤li≤ri≤n1≤li≤ri≤n).

Output

Print one integer — maximum number of painted sections if you hire q−2q−2 painters.

Examples

Input
7 5
1 4
4 5
5 6
6 7
3 5
Output
7
Input
4 3
1 1
2 2
3 4
Output
2
Input
4 4
1 1
2 2
2 3
3 4
Output
3

题解:题意就是给你n段区间,然后给你一个大区间,让你用最多n-2段小区间去覆盖大区间,问你:最多可以覆盖大区间多少个点;
我们先按区间右侧从小到大排序;记录每一个点覆盖的次数为1和2的点;
然后暴力O(n^2)枚举2个小区间,分别处理呗覆盖1和2的点即可; 参考代码:
 #include<bits/stdc++.h>
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<map>
#include<algorithm>
using namespace std;
#define clr(a,v) memset(a,v,sizeof(a))
#define PI acos(-1.0)
#define PII pair<int,int>
#define pb push_back
#define fi first
#define se second
typedef pair<int,int> pii;
typedef long long ll;
const int maxn=;
int l[maxn],r[maxn],cnt[maxn],sum[][maxn];
pii sa[maxn];
int main()
{
int n,q;
scanf("%d%d",&n,&q);
clr(cnt,);clr(sum,);
for(int i=;i<=q;++i)
{
scanf("%d%d",&l[i],&r[i]);
sa[i].fi=r[i];sa[i].se=l[i];
for(int j=l[i];j<=r[i];++j) cnt[j]++;
}
sort(sa+,sa++q);
for(int i=;i<=n;++i)
{
sum[][i]=sum[][i-];
sum[][i]=sum[][i-];
if(cnt[i]==) sum[][i]++;
if(cnt[i]==) sum[][i]++;
}
for(int i=;i<=q;++i) r[i]=sa[i].fi,l[i]=sa[i].se;
int ans=;
for(int i=;i<=n;++i) {if(cnt[i]) ans++;}
int cans=ans;
for(int i=;i<=q;++i)
{
for(int j=i+;j<=q;++j)
{
if(l[j]>r[i]) cans=min(cans,(sum[][r[i]]-sum[][l[i]-])+(sum[][r[j]]-sum[][l[j]-]));
else if(l[j]<=r[i]&&l[j]>=l[i]) cans=min(cans,(sum[][r[j]]-sum[][l[i]-])+(sum[][r[i]]-sum[][l[j]-]));
else cans=min(cans,(sum[][r[j]]-sum[][l[j]-])+(sum[][r[i]]-sum[][l[i]-]));
}
}
printf("%d\n",ans-cans);
return ;
}

D.Stressful Training

Berland SU holds yet another training contest for its students today. nn students came, each of them brought his laptop. However, it turned out that everyone has forgot their chargers!

Let students be numbered from 11 to nn. Laptop of the ii-th student has charge aiai at the beginning of the contest and it uses bibi of charge per minute (i.e. if the laptop has cc charge at the beginning of some minute, it becomes c−bic−bi charge at the beginning of the next minute). The whole contest lasts for kk minutes.

Polycarp (the coach of Berland SU) decided to buy a single charger so that all the students would be able to successfully finish the contest. He buys the charger at the same moment the contest starts.

Polycarp can choose to buy the charger with any non-negative (zero or positive) integer power output. The power output is chosen before the purchase, it can't be changed afterwards. Let the chosen power output be xx. At the beginning of each minute (from the minute contest starts to the last minute of the contest) he can plug the charger into any of the student's laptops and use it for some integernumber of minutes. If the laptop is using bibi charge per minute then it will become bi−xbi−x per minute while the charger is plugged in. Negative power usage rate means that the laptop's charge is increasing. The charge of any laptop isn't limited, it can become infinitely large. The charger can be plugged in no more than one laptop at the same time.

The student successfully finishes the contest if the charge of his laptop never is below zero at the beginning of some minute (from the minute contest starts to the last minute of the contest, zero charge is allowed). The charge of the laptop of the minute the contest ends doesn't matter.

Help Polycarp to determine the minimal possible power output the charger should have so that all the students are able to successfully finish the contest. Also report if no such charger exists.

Input

The first line contains two integers nn and kk (1≤n≤2⋅1051≤n≤2⋅105, 1≤k≤2⋅1051≤k≤2⋅105) — the number of students (and laptops, correspondigly) and the duration of the contest in minutes.

The second line contains nn integers a1,a2,…,ana1,a2,…,an (1≤ai≤10121≤ai≤1012) — the initial charge of each student's laptop.

The third line contains nn integers b1,b2,…,bnb1,b2,…,bn (1≤bi≤1071≤bi≤107) — the power usage of each student's laptop.

Output

Print a single non-negative integer — the minimal possible power output the charger should have so that all the students are able to successfully finish the contest.

If no such charger exists, print -1.

Examples

Input
2 4
3 2
4 2
Output
5
Input
1 5
4
2
Output
1
Input
1 6
4
2
Output
2
Input
2 2
2 10
3 15
Output
-1

Let's take a look at the state of laptops in the beginning of each minute on the first example with the charger of power 55:

  1. charge: [3,2][3,2], plug the charger into laptop 1;
  2. charge: [3−4+5,2−2]=[4,0][3−4+5,2−2]=[4,0], plug the charger into laptop 2;
  3. charge: [4−4,0−2+5]=[0,3][4−4,0−2+5]=[0,3], plug the charger into laptop 1;
  4. charge: [0−4+5,3−2]=[1,1][0−4+5,3−2]=[1,1].

The contest ends after the fourth minute.

However, let's consider the charger of power 44:

  1. charge: [3,2][3,2], plug the charger into laptop 1;
  2. charge: [3−4+4,2−2]=[3,0][3−4+4,2−2]=[3,0], plug the charger into laptop 2;
  3. charge: [3−4,0−2+4]=[−1,2][3−4,0−2+4]=[−1,2], the first laptop has negative charge, thus, the first student doesn't finish the contest.

In the fourth example no matter how powerful the charger is, one of the students won't finish the contest.


题解:二分答案;
参考代码:
 #include <bits/stdc++.h>
#define int long long
using namespace std; const int maxN = ;
int a[maxN], b[maxN];
int n, k; bool ok(int m) {
set<int> rest;
for (int i = ; i < k; i++)
rest.insert(i);
for (int i = ; i < n; i++) {
for (int t = a[i]; t / b[i] < k; t += m) {
auto it = rest.upper_bound(t / b[i]);
if (it == rest.begin())
return false;
rest.erase(--it);
}
}
return true;
} signed main() {
ios::sync_with_stdio(false);
cin >> n >> k; --k;
for (int i = ; i < n; i++)
cin >> a[i];
for (int i = ; i < n; i++)
cin >> b[i];
int l = -, r = 2e12;
while (l + != r) {
int m = (l + r) / ;
ok(m) ? r = m : l = m;
}
cout << (ok(r) ? r : -) << endl;
return ;
}

E.Knapsack

You have a set of items, each having some integer weight not greater than 88. You denote that a subset of items is good if total weight of items in the subset does not exceed WW.

You want to calculate the maximum possible weight of a good subset of items. Note that you have to consider the empty set and the original set when calculating the answer.

Input

The first line contains one integer WW (0≤W≤10180≤W≤1018) — the maximum total weight of a good subset.

The second line denotes the set of items you have. It contains 88 integers cnt1cnt1, cnt2cnt2, ..., cnt8cnt8 (0≤cnti≤10160≤cnti≤1016), where cnticnti is the number of items having weight iiin the set.

Output

Print one integer — the maximum possible weight of a good subset of items.

Examples

Input
10
1 2 3 4 5 6 7 8
Output
10
Input
0
0 0 0 0 0 0 0 0
Output
0
Input
3
0 4 1 0 0 9 8 3
Output
3

题解:题意就是给你8个数,第i个数代表i有多少个;给你你个数n;
然后让你求:由这些数组成小与等于n的最大值是多少;
dfs搜索,从大到小搜;
参考代码:
 #include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const double PI = acos(-1.0);
const double eps = 1e-;
const int INF = 0x3f3f3f3f;
ll W,ans,cnt[];
void work(ll &a,ll b){if(a<b) a=b;}
void dfs(ll x,ll now)
{
if(x==){work(ans,now);return ;}
for(ll i=,v=min((W-now)/x,1ll*cnt[x]);i;--i,--v) dfs(x+,now+max(*1ll,v*x));
}
int main()
{
scanf("%lld",&W); ans=;
for(int i=;i<=;++i) scanf("%lld",&cnt[i]);
dfs(,);
printf("%lld\n",ans);
return ;
}

F.Clear String

You are given a string ss of length nn consisting of lowercase Latin letters. You may apply some operations to this string: in one operation you can delete some contiguous substring of this string, if all letters in the substring you delete are equal. For example, after deleting substring bbbb from string abbbbaccdd we get the string aaccdd.

Calculate the minimum number of operations to delete the whole string ss.

Input

The first line contains one integer nn (1≤n≤5001≤n≤500) — the length of string ss.

The second line contains the string ss (|s|=n|s|=n) consisting of lowercase Latin letters.

Output

Output a single integer — the minimal number of operation to delete string ss.

Examples

Input
5
abaca
Output
3
Input
8
abcddcba
Output
4

题解:题意就是你每次可以将一段区间的颜色变为一种颜色;
问你:变成给定样子最少需要多少步;
数位DP;
dp[i][j]:表示把i~j处理好至少需要多少次;
转移方程:
dp[l][r]=min(dp[l][r],dp[l+1][r]+(s[l]!=s[l+1]));
dp[l][r]=min(dp[l][r],dp[l+1][r]+(s[l]!=s[r]));
dp[l][r]=min(dp[l][r],dp[l][r-1]+(s[r]!=s[r-1]));
dp[l][r]=min(dp[l][r],dp[l][r-1]+(s[l]!=s[r]));

参考代码:

 #include<bits/stdc++.h>
using namespace std;
const int INF=0x3f3f3f3f;
int dp[][],n;
char s[]; int main()
{
scanf("%d",&n); scanf("%s",s+);
for(int i=;i<=n;++i) dp[i][i]=;
for(int i=;i<=n;++i)
{
for(int l=;l<=n-i+;++l)
{
int r=l+i-;
dp[l][r]=INF;
dp[l][r]=min(dp[l][r],dp[l+][r]+(s[l]!=s[l+]));
dp[l][r]=min(dp[l][r],dp[l+][r]+(s[l]!=s[r])); dp[l][r]=min(dp[l][r],dp[l][r-]+(s[r]!=s[r-]));
dp[l][r]=min(dp[l][r],dp[l][r-]+(s[l]!=s[r]));
for(int j=l;j<=r-;++j) dp[l][r]=min(dp[l][r],dp[l][j]+dp[j+][r]);
}
}
printf("%d\n",dp[][n]);
return ;
}

CF 1132A,1132B,1132C,1132D,1132E,1132F(Round 61 A,B,C,D,E,F)题解的更多相关文章

  1. Educational Codeforces Round 61 (Rated for Div. 2) D,F题解

    D. Stressful Training 题目链接:https://codeforces.com/contest/1132/problem/D 题意: 有n台电脑,每台电脑都有初始电量ai,也有一个 ...

  2. cf之路,1,Codeforces Round #345 (Div. 2)

     cf之路,1,Codeforces Round #345 (Div. 2) ps:昨天第一次参加cf比赛,比赛之前为了熟悉下cf比赛题目的难度.所以做了round#345连试试水的深浅.....   ...

  3. Codeforces Beta Round #61 (Div. 2)

    Codeforces Beta Round #61 (Div. 2) http://codeforces.com/contest/66 A 输入用long double #include<bit ...

  4. Educational Codeforces Round 61

    Educational Codeforces Round 61 今早刚刚说我适合打pikmike出的EDU 然后我就挂了 A 不管 B 不管 C 这道题到快结束了才调出来 大概就是\(n^2\)枚举不 ...

  5. Codeforces Round #524 (Div. 2)(前三题题解)

    这场比赛手速场+数学场,像我这样读题都读不大懂的蒟蒻表示呵呵呵. 第四题搞了半天,大概想出来了,但来不及(中途家里网炸了)查错,于是我交了两次丢了100分.幸亏这次没有掉rating. 比赛传送门:h ...

  6. 【打CF,学算法——三星级】Codeforces Round #313 (Div. 2) C. Gerald&#39;s Hexagon

    [CF简单介绍] 提交链接:http://codeforces.com/contest/560/problem/C 题面: C. Gerald's Hexagon time limit per tes ...

  7. CF&&CC百套计划3 Codeforces Round #204 (Div. 1) A. Jeff and Rounding

    http://codeforces.com/problemset/problem/351/A 题意: 2*n个数,选n个数上取整,n个数下取整 最小化 abs(取整之后数的和-原来数的和) 先使所有的 ...

  8. CF&&CC百套计划1 Codeforces Round #449 B. Ithea Plays With Chtholly

    http://codeforces.com/contest/896/problem/B 题意: 交互题 n张卡片填m个1到c之间的数,1<=n*ceil(c/2)<=m 最后填出一个单调非 ...

  9. 【打CF,学算法——二星级】Codeforces Round #313 (Div. 2) B. Gerald is into Art(水题)

    [CF简单介绍] 提交链接:http://codeforces.com/contest/560/problem/B 题面: B. Gerald is into Art time limit per t ...

随机推荐

  1. C语言程序设计100例之(10):最大公约数

    例10        最大公约数 问题描述 有三个正整数a,b,c(0<a,b,c<10^6),其中c不等于b.若a和c的最大公约数为b,现已知a和b,求满足条件的最小的c. 输入数据 第 ...

  2. 【algo&ds】8.最小生成树

    1.最小生成树介绍 什么是最小生成树? 最小生成树(Minimum spanning tree,MST)是在一个给定的无向图G(V,E)中求一棵树T,使得这棵树拥有图G中的所有顶点,且所有边都是来自图 ...

  3. 并行模式之Master-Worker模式

    并行模式之Master-Worker模式 一).Master-Worker模式 作用: 将一个大任务分解成若干个小任务,分发给多个子线程执行. 注: 将大任务分解成小任务,小任务的实现逻辑要相同. 二 ...

  4. C++控制台闪回;编译器警告C4305,C4244

    这是我以前解决问题时,收集在印象笔记里的内容,为了以后整理方便,把它转移至这里.以下内容,均来自微软官方网站相关.     问题:C++控制台闪回     解决办法: 1,在程序结尾添加system( ...

  5. LMS自适应天线阵列设计 MATLAB

    在自适应天线课上刚刚学了LMS自适应阵,先出一个抢先版贴一下结果,抢先某个小朋友一步. 关于LMS的具体介绍,直接看wiki里的吧,解释的比书上简明:传送门:https://en.wikipedia. ...

  6. Res2net:多尺度骨干网络结构

    <Res2Net: A New Multi-scale Backbone Architecture> 来自:南开大学程明明组 论文:https://arxiv.org/abs/1904.0 ...

  7. SpringBoot之DispatcherServlet详解及源码解析

    在使用SpringBoot之后,我们表面上已经无法直接看到DispatcherServlet的使用了.本篇文章,带大家从最初DispatcherServlet的使用开始到SpringBoot源码中Di ...

  8. es6 every的使用

    let arr2 =[1,3,5,7,9,10]; //arr2.every() 数组里面所有的元素都有符合条件,才返回true var b =arr2.every(function (val,ind ...

  9. 解决Debina系统自动更新软件包的问题

    不知从何时开始,我的电脑每天开机连接上网络之后,不断的在下载数据,状态栏显示网速达到每秒1到2兆.开始我还不太在意,不过后来由于带宽全部被这种莫名其奥妙的下载占据了,我连网页都无否正常浏览了,所以我决 ...

  10. Apache用户认证、域名跳转、Apache访问日志

    5月29日任务 课程内容: 11.18 Apache用户认证11.19/11.20 域名跳转11.21 Apache访问日志扩展 apache虚拟主机开启php的短标签 http://ask.apel ...