Codeforces Round #625 (Div. 2) A ~ D

                                                                       A. Contest for Robots

time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

Polycarp is preparing the first programming contest for robots. There are nn problems in it, and a lot of robots are going to participate in it. Each robot solving the problem ii gets pipi points, and the score of each robot in the competition is calculated as the sum of pipi over all problems ii solved by it. For each problem, pipi is an integer not less than 11.

Two corporations specializing in problem-solving robot manufacturing, "Robo-Coder Inc." and "BionicSolver Industries", are going to register two robots (one for each corporation) for participation as well. Polycarp knows the advantages and flaws of robots produced by these companies, so, for each problem, he knows precisely whether each robot will solve it during the competition. Knowing this, he can try predicting the results — or manipulating them.

For some reason (which absolutely cannot involve bribing), Polycarp wants the "Robo-Coder Inc." robot to outperform the "BionicSolver Industries" robot in the competition. Polycarp wants to set the values of pipi in such a way that the "Robo-Coder Inc." robot gets strictly more points than the "BionicSolver Industries" robot. However, if the values of pipi will be large, it may look very suspicious — so Polycarp wants to minimize the maximum value of pipi over all problems. Can you help Polycarp to determine the minimum possible upper bound on the number of points given for solving the problems?

Input

The first line contains one integer nn (1≤n≤1001≤n≤100) — the number of problems.

The second line contains nn integers r1r1, r2r2, ..., rnrn (0≤ri≤10≤ri≤1). ri=1ri=1 means that the "Robo-Coder Inc." robot will solve the ii-th problem, ri=0ri=0 means that it won't solve the ii-th problem.

The third line contains nn integers b1b1, b2b2, ..., bnbn (0≤bi≤10≤bi≤1). bi=1bi=1 means that the "BionicSolver Industries" robot will solve the ii-th problem, bi=0bi=0 means that it won't solve the ii-th problem.

Output

If "Robo-Coder Inc." robot cannot outperform the "BionicSolver Industries" robot by any means, print one integer −1−1.

Otherwise, print the minimum possible value of maxi=1npimaxi=1npi, if all values of pipi are set in such a way that the "Robo-Coder Inc." robot gets strictly more points than the "BionicSolver Industries" robot.

Examples

input

Copy

5
1 1 1 0 0
0 1 1 1 1

output

Copy

3

input

Copy

3
0 0 0
0 0 0

output

Copy

-1

input

Copy

4
1 1 1 1
1 1 1 1

output

Copy

-1

input

Copy

9
1 0 0 0 0 0 0 0 1
0 1 1 0 1 1 1 1 0

output

Copy

4

Note

In the first example, one of the valid score assignments is p=[3,1,3,1,1]p=[3,1,3,1,1]. Then the "Robo-Coder" gets 77 points, the "BionicSolver" — 66 points.

In the second example, both robots get 00 points, and the score distribution does not matter.

In the third example, both robots solve all problems, so their points are equal.

題目大意 :

輸入兩組數,爲1表示有該任務,0表示沒有,現在由你定義每個任務的分數,使得最高分最小的情況下,第一個人的總分大於第二個人,如果無法滿足,輸出-1

思路 :

先處理兩個人的特有任務,如果第一個人特有的任務第二個人都有,那麼一定無法滿足,因爲兩個人任務的分數是相同的,第二個人特有的任務按照題目要求,應當爲1分,第一個人的特有任務,按照第二個人特有任務的數量,平均分配就好,注意特判能夠整除的情況

Accepted code

#include<bits/stdc++.h>
#include<unordered_map>
using namespace std;

#define sc scanf
#define ls rt << 1
#define rs ls | 1
#define Min(x, y) x = min(x, y)
#define Max(x, y) x = max(x, y)
#define ALL(x) (x).begin(),(x).end()
#define SZ(x) ((int)(x).size())
#define pir pair <int, int>
#define MK(x, y) make_pair(x, y)
#define MEM(x, b) memset(x, b, sizeof(x))
#define lowbit(x) ((x) & -(x))
#define P2(x) ((x) * (x))

typedef long long ll;
const int MOD = 1e9 + 7;
const int MAXN = 2e5 + 100;
const int INF = 0x3f3f3f3f;
inline ll fpow(ll a, ll b){ ll r = 1, t = a; while (b){ if (b & 1)r = (r*t) % MOD; b >>= 1; t = (t*t) % MOD; }return r; }

int a[MAXN], b[MAXN], n;
int ans1, ans2; 

int main()
{
	cin >> n;
	for (int i = 1; i <= n; i++)
		cin >> a[i];
	for (int i = 1; i <= n; i++)
		cin >> b[i];
	for (int i = 1; i <= n; i++) {
		if (a[i] && !b[i])
			ans1++;
		else if (!a[i] && b[i])
			ans2++;
	}
	if (ans1 == 0)
		cout << -1 << endl, exit(0);   // 第一個人特有任務
	if (ans1 > ans2)
		cout << 1 << endl, exit(0);   
	if (ans2 % ans1 == 0)
		cout << ans2 / ans1 + 1 << endl;
	else
		cout << (ans2 - 1) / ans1 + 1 << endl;
	return 0;  // 改數組大小!!!
}

                                                                              B. Journey Planning

 

Tanya wants to go on a journey across the cities of Berland. There are nn cities situated along the main railroad line of Berland, and these cities are numbered from 11 to nn.

Tanya plans her journey as follows. First of all, she will choose some city c1c1 to start her journey. She will visit it, and after that go to some other city c2>c1c2>c1, then to some other city c3>c2c3>c2, and so on, until she chooses to end her journey in some city ck>ck−1ck>ck−1. So, the sequence of visited cities [c1,c2,…,ck][c1,c2,…,ck] should be strictly increasing.

There are some additional constraints on the sequence of cities Tanya visits. Each city ii has a beauty value bibi associated with it. If there is only one city in Tanya's journey, these beauty values imply no additional constraints. But if there are multiple cities in the sequence, then for any pair of adjacent cities cici and ci+1ci+1, the condition ci+1−ci=bci+1−bcici+1−ci=bci+1−bci must hold.

For example, if n=8n=8 and b=[3,4,4,6,6,7,8,9]b=[3,4,4,6,6,7,8,9], there are several three possible ways to plan a journey:

  • c=[1,2,4]c=[1,2,4];
  • c=[3,5,6,8]c=[3,5,6,8];
  • c=[7]c=[7] (a journey consisting of one city is also valid).

There are some additional ways to plan a journey that are not listed above.

Tanya wants her journey to be as beautiful as possible. The beauty value of the whole journey is the sum of beauty values over all visited cities. Can you help her to choose the optimal plan, that is, to maximize the beauty value of the journey?

Input

The first line contains one integer nn (1≤n≤2⋅1051≤n≤2⋅105) — the number of cities in Berland.

The second line contains nn integers b1b1, b2b2, ..., bnbn (1≤bi≤4⋅1051≤bi≤4⋅105), where bibi is the beauty value of the ii-th city.

Output

Print one integer — the maximum beauty of a journey Tanya can choose.

Examples

Input

6
10 7 1 9 10 15

Output

26

Input

1
400000

Output

400000

Input

7
8 9 26 11 12 29 14

Output

55

Note

The optimal journey plan in the first example is c=[2,4,5]c=[2,4,5].

The optimal journey plan in the second example is c=[1]c=[1].

The optimal journey plan in the third example is c=[3,6]c=[3,6].

題目大意 :

有N個數,對於Ai和Aj這兩個數,如果滿足Ai - Aj = i - j,那麼兩個數就可以求和,輸出整個數組能夠求和的最大值

思路 :

將題目給的式子移項變成,Ai - i = Aj - j,所以權值 - 下標相等的爲一組,這樣的話開一個map存一下就行了

Accepted code

#include<bits/stdc++.h>
#include<unordered_map>
using namespace std;

#define sc scanf
#define ls rt << 1
#define rs ls | 1
#define Min(x, y) x = min(x, y)
#define Max(x, y) x = max(x, y)
#define ALL(x) (x).begin(),(x).end()
#define SZ(x) ((int)(x).size())
#define pir pair <int, int>
#define MK(x, y) make_pair(x, y)
#define MEM(x, b) memset(x, b, sizeof(x))
#define lowbit(x) ((x) & -(x))
#define P2(x) ((x) * (x))

typedef long long ll;
const int MOD = 1e9 + 7;
const int MAXN = 2e5 + 100;
const int INF = 0x3f3f3f3f;
inline ll fpow(ll a, ll b){ ll r = 1, t = a; while (b){ if (b & 1)r = (r*t) % MOD; b >>= 1; t = (t*t) % MOD; }return r; }

int n;
unordered_map <int, ll> mp;

int main()
{
	cin >> n;
	for (int i = 1; i <= n; i++) {
		ll t;
		sc("%lld", &t);
		mp[t - i] += t;
	}
	ll max_ = -1;
	for (auto it : mp)
		Max(max_, it.second);
	cout << max_ << endl;
	return 0;  // 改數組大小!!!
}

                                                                                    C. Remove Adjacent

You are given a string ss consisting of lowercase Latin letters. Let the length of ss be |s||s|. You may perform several operations on this string.

In one operation, you can choose some index ii and remove the ii-th character of ss (sisi) if at least one of its adjacent characters is the previous letter in the Latin alphabet for sisi. For example, the previous letter for b is a, the previous letter for s is r, the letter a has no previous letters. Note that after each removal the length of the string decreases by one. So, the index ii should satisfy the condition 1≤i≤|s|1≤i≤|s| during each operation.

For the character sisi adjacent characters are si−1si−1 and si+1si+1. The first and the last characters of ss both have only one adjacent character (unless |s|=1|s|=1).

Consider the following example. Let s=s= bacabcab.

  1. During the first move, you can remove the first character s1=s1= b because s2=s2= a. Then the string becomes s=s= acabcab.
  2. During the second move, you can remove the fifth character s5=s5= c because s4=s4= b. Then the string becomes s=s= acabab.
  3. During the third move, you can remove the sixth character s6=s6='b' because s5=s5= a. Then the string becomes s=s= acaba.
  4. During the fourth move, the only character you can remove is s4=s4= b, because s3=s3= a (or s5=s5= a). The string becomes s=s= acaa and you cannot do anything with it.

Your task is to find the maximum possible number of characters you can remove if you choose the sequence of operations optimally.

Input

The first line of the input contains one integer |s||s| (1≤|s|≤1001≤|s|≤100) — the length of ss.

The second line of the input contains one string ss consisting of |s||s| lowercase Latin letters.

Output

Print one integer — the maximum possible number of characters you can remove if you choose the sequence of moves optimally.

Examples

Input

8
bacabcab

Output

4

Input

4
bcda

Output

3

Input

6
abbbbb

Output

5

Note

The first example is described in the problem statement. Note that the sequence of moves provided in the statement is not the only, but it can be shown that the maximum possible answer to this test is 44.

In the second example, you can remove all but one character of ss. The only possible answer follows.

  1. During the first move, remove the third character s3=s3= d, ss becomes bca.
  2. During the second move, remove the second character s2=s2= c, ss becomes ba.
  3. And during the third move, remove the first character s1=s1= b, ss becomes a.

題目大意 :

一個字符串,如果與某個位置相鄰的字母,至少有一個比該位置的字母小1,那麼該位置的字母就可以刪掉,剩下的字母接上來,輸出對於整個字符串,最多可以刪多少個字母。

思路 :

數據很小,可以直接暴力,不難想到先刪字母大的,然後再刪小的可以保證刪的最多,所以就從 ‘z' 遍歷到 ’a‘, 一個一個刪,當前字母不能刪就往前循環

Accepted code

#include<bits/stdc++.h>
#include<unordered_map>
using namespace std;

#define sc scanf
#define ls rt << 1
#define rs ls | 1
#define Min(x, y) x = min(x, y)
#define Max(x, y) x = max(x, y)
#define ALL(x) (x).begin(),(x).end()
#define SZ(x) ((int)(x).size())
#define pir pair <int, int>
#define MK(x, y) make_pair(x, y)
#define MEM(x, b) memset(x, b, sizeof(x))
#define lowbit(x) ((x) & -(x))
#define P2(x) ((x) * (x))

typedef long long ll;
const int MOD = 1e9 + 7;
const int MAXN = 2e5 + 100;
const int INF = 0x3f3f3f3f;
inline ll fpow(ll a, ll b){ ll r = 1, t = a; while (b){ if (b & 1)r = (r*t) % MOD; b >>= 1; t = (t*t) % MOD; }return r; }

string str;
int n;

int main()
{
	cin >> n >> str;
	for (char i = 'z'; i >= 'b'; i--) {
		if (SZ(str) == 1)    // 無法刪除
			break;
		while (true) {
			if (SZ(str) == 1)
				break;
			bool ok = false;
			for (int j = 0; j < SZ(str); j++) {
				if (str[j] != i)                  // 只刪當前字母
					continue;
				if (j == 0) {                     // 開頭字母相鄰的要特判
					if (str[j + 1] == i - 1) {
						ok = true;
						str.erase(str.begin() + j, str.begin() + j + 1);
						break;
					}
				}
				else if (j == SZ(str) - 1) {      // 最後一個字母相鄰的特判
					if (str[j - 1] == i - 1) {
						ok = true;
						str.erase(str.begin() + j, str.begin() + j + 1);
						break;
					}
				}
				else {
					if (str[j - 1] == i - 1 || str[j + 1] == i - 1) {
						ok = true;
						str.erase(str.begin() + j, str.begin() + j + 1);
						break;
					}
				}
			}
			if (!ok)
				break;
		}
	}
	cout << n - SZ(str) << endl;
	return 0;  // 改數組大小!!!
}

                                                                       D. Navigation System

The map of Bertown can be represented as a set of nn intersections, numbered from 11 to nn and connected by mm one-way roads. It is possible to move along the roads from any intersection to any other intersection. The length of some path from one intersection to another is the number of roads that one has to traverse along the path. The shortest path from one intersection vv to another intersection uu is the path that starts in vv, ends in uu and has the minimum length among all such paths.

Polycarp lives near the intersection ss and works in a building near the intersection tt. Every day he gets from ss to tt by car. Today he has chosen the following path to his workplace: p1p1, p2p2, ..., pkpk, where p1=sp1=s, pk=tpk=t, and all other elements of this sequence are the intermediate intersections, listed in the order Polycarp arrived at them. Polycarp never arrived at the same intersection twice, so all elements of this sequence are pairwise distinct. Note that you know Polycarp's path beforehand (it is fixed), and it is not necessarily one of the shortest paths from ss to tt.

Polycarp's car has a complex navigation system installed in it. Let's describe how it works. When Polycarp starts his journey at the intersection ss, the system chooses some shortest path from ss to tt and shows it to Polycarp. Let's denote the next intersection in the chosen path as vv. If Polycarp chooses to drive along the road from ss to vv, then the navigator shows him the same shortest path (obviously, starting from vv as soon as he arrives at this intersection). However, if Polycarp chooses to drive to another intersection ww instead, the navigator rebuilds the path: as soon as Polycarp arrives at ww, the navigation system chooses some shortest path from ww to tt and shows it to Polycarp. The same process continues until Polycarp arrives at tt: if Polycarp moves along the road recommended by the system, it maintains the shortest path it has already built; but if Polycarp chooses some other path, the system rebuilds the path by the same rules.

Here is an example. Suppose the map of Bertown looks as follows, and Polycarp drives along the path [1,2,3,4][1,2,3,4] (s=1s=1, t=4t=4):

Check the picture by the link http://tk.codeforces.com/a.png

  1. When Polycarp starts at 11, the system chooses some shortest path from 11 to 44. There is only one such path, it is [1,5,4][1,5,4];
  2. Polycarp chooses to drive to 22, which is not along the path chosen by the system. When Polycarp arrives at 22, the navigator rebuilds the path by choosing some shortest path from 22 to 44, for example, [2,6,4][2,6,4] (note that it could choose [2,3,4][2,3,4]);
  3. Polycarp chooses to drive to 33, which is not along the path chosen by the system. When Polycarp arrives at 33, the navigator rebuilds the path by choosing the only shortest path from 33 to 44, which is [3,4][3,4];
  4. Polycarp arrives at 44 along the road chosen by the navigator, so the system does not have to rebuild anything.

Overall, we get 22 rebuilds in this scenario. Note that if the system chose [2,3,4][2,3,4] instead of [2,6,4][2,6,4] during the second step, there would be only 11 rebuild (since Polycarp goes along the path, so the system maintains the path [3,4][3,4] during the third step).

The example shows us that the number of rebuilds can differ even if the map of Bertown and the path chosen by Polycarp stays the same. Given this information (the map and Polycarp's path), can you determine the minimum and the maximum number of rebuilds that could have happened during the journey?

Input

The first line contains two integers nn and mm (2≤n≤m≤2⋅1052≤n≤m≤2⋅105) — the number of intersections and one-way roads in Bertown, respectively.

Then mm lines follow, each describing a road. Each line contains two integers uu and vv (1≤u,v≤n1≤u,v≤n, u≠vu≠v) denoting a road from intersection uu to intersection vv. All roads in Bertown are pairwise distinct, which means that each ordered pair (u,v)(u,v) appears at most once in these mm lines (but if there is a road (u,v)(u,v), the road (v,u)(v,u) can also appear).

The following line contains one integer kk (2≤k≤n2≤k≤n) — the number of intersections in Polycarp's path from home to his workplace.

The last line contains kk integers p1p1, p2p2, ..., pkpk (1≤pi≤n1≤pi≤n, all these integers are pairwise distinct) — the intersections along Polycarp's path in the order he arrived at them. p1p1 is the intersection where Polycarp lives (s=p1s=p1), and pkpk is the intersection where Polycarp's workplace is situated (t=pkt=pk). It is guaranteed that for every i∈[1,k−1]i∈[1,k−1] the road from pipi to pi+1pi+1 exists, so the path goes along the roads of Bertown.

Output

Print two integers: the minimum and the maximum number of rebuilds that could have happened during the journey.

Examples

Input

6 9
1 5
5 4
1 2
2 3
3 4
4 1
2 6
6 4
4 2
4
1 2 3 4

Output

1 2

Input

7 7
1 2
2 3
3 4
4 5
5 6
6 7
7 1
7
1 2 3 4 5 6 7

Output

0 0

Input

8 13
8 7
8 6
7 5
7 4
6 5
6 4
5 3
5 2
4 3
4 2
3 1
2 1
1 8
5
8 7 5 2 1

Output

0 3

這道題比賽的時候讀錯了題T_T,補題的時候也想了很久沒做出來(題都錯了咋可能做對),後來理解了正確的題意就馬上給秒了

題目大意 :

給你一個有向圖和一條路徑,你可以腦補出一個導航系統,每到一個點,就能顯示出從該點到終點的最短路,最短路不一定唯一,所以導航的路線也可能不唯一,如果你下一個點走的仍然是你當前顯示的最短路上的點,那麼導航到下一個點就沒有改變,最後輸出導航顯示最短路最少和最多的可能變化次數。

思路 :

導航改變的原因,就是因爲最短路不唯一,考慮下最少改變次數,一定是你沒有按照上一次的最短路走,也就是說,你這次所在的點,到終點的最短路 ≥ 上一個點到終點的最短路,最多改變次數,多出來的就是當前是最短路,但是不唯一,這樣的話,很容易想到反向建圖,求出終點到每個點的最短路實際上就是每個點到終點的最短路,過程中dp一下,將不唯一的標記出來,因爲邊權都是1,所以先到的一定先標記,不用考慮從其他地方到該點更短了。

Accepted code

#include<bits/stdc++.h>
#include<unordered_map>
using namespace std;

#define sc scanf
#define ls rt << 1
#define rs ls | 1
#define Min(x, y) x = min(x, y)
#define Max(x, y) x = max(x, y)
#define ALL(x) (x).begin(),(x).end()
#define SZ(x) ((int)(x).size())
#define pir pair <int, int>
#define MK(x, y) make_pair(x, y)
#define MEM(x, b) memset(x, b, sizeof(x))
#define lowbit(x) ((x) & -(x))
#define P2(x) ((x) * (x))

typedef long long ll;
const int MOD = 1e9 + 7;
const int MAXN = 2e5 + 100;
const int INF = 0x3f3f3f3f;
inline ll fpow(ll a, ll b){ ll r = 1, t = a; while (b){ if (b & 1)r = (r*t) % MOD; b >>= 1; t = (t*t) % MOD; }return r; }

vector <int> G[MAXN];
int dis[MAXN], dp[MAXN], n, m;
int pre[MAXN], k;
bool vis[MAXN];

void bfs(int x) {
	queue <int> q;
	MEM(dis, INF); dis[x] = 0;
	q.push(x);
	while (!q.empty()) {
		int ui = q.front();
		q.pop();
		if (vis[ui])
			continue;
		vis[ui] = true;
		for (auto it : G[ui]) {
			int vi = it;
			if (dis[vi] == dis[ui] + 1)
				dp[vi]++;
			Min(dis[vi], dis[ui] + 1);
			if (!vis[vi])
				q.push(vi);
		}
	}
}

int main()
{
	//freopen("D:/input.txt", "r", stdin);
	cin >> n >> m;
	for (int i = 0; i < m; i++) {
		int ui, vi;
		sc("%d %d", &ui, &vi);    // 多到一,反向建邊即可
		G[vi].push_back(ui);
	}
	cin >> k;
	for (int i = 1; i <= k; i++)
		sc("%d", &pre[i]);
	bfs(pre[k]);
	int ans1 = 0, ans2 = 0;
	for (int i = 1; i < k; i++) {
		if (dis[pre[i + 1]] >= dis[pre[i]])  // 沒走最短路,必須改變
			ans1++, ans2++;
		else if (dp[pre[i]])               // 最短路不唯一,可能改變
			ans2++;
	}
	cout << ans1 << " " << ans2 << endl;
	return 0;  // 改數組大小!!!
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章