Codeforces Round #443 (Div. 2)ABC

A. Borya‘s Diagnosis

It seems that Borya is seriously sick. He is going visit n doctors to find out the exact diagnosis. Each of the doctors needs the information about all previous visits, so Borya has to visit them in the prescribed order (i.e. Borya should first visit doctor 1, then doctor 2, then doctor 3 and so on). Borya will get the information about his health from the last doctor.

Doctors have a strange working schedule. The doctor i goes to work on the si-th day and works every di day. So, he works on days si, si + di, si + 2di, ....

The doctor‘s appointment takes quite a long time, so Borya can not see more than one doctor per day. What is the minimum time he needs to visit all doctors?

Input

First line contains an integer n — number of doctors (1 ≤ n ≤ 1000).

Next n lines contain two numbers si and di (1 ≤ si, di ≤ 1000).

Output

Output a single integer — the minimum day at which Borya can visit the last doctor.

Examples

Input

32 21 22 2

Output

4

Input

210 16 5

Output

11

Note

In the first sample case, Borya can visit all doctors on days 2, 3 and 4.

In the second sample case, Borya can visit all doctors on days 10 and 11.

模拟一下。

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3
 4 int main() {
 5     int n, ans = 0;
 6     cin >> n;
 7     while(n--) {
 8         int s, d;
 9         cin >> s >>d;
10         while(ans >= s) s += d;
11         ans = s;
12     }
13     printf("%d\n",ans);
14     return 0;
15 }

B. Table Tennis

n
people are standing in a line to play table tennis. At first, the first
two players in the line play a game. Then the loser goes to the end of
the line, and the winner plays with the next person from the line, and
so on. They play until someone wins k games in a row. This player becomes the winner.

For
each of the participants, you know the power to play table tennis, and
for all players these values are different. In a game the player with
greater power always wins. Determine who will be the winner.

Input

The first line contains two integers: n and k (2 ≤ n ≤ 500, 2 ≤ k ≤ 1012) — the number of people and the number of wins.

The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ n) — powers of the player. It‘s guaranteed that this line contains a valid permutation, i.e. all ai are distinct.

Output

Output a single integer — power of the winner.

Examples

Input

2 21 2

Output

2 

Input

4 23 1 2 4

Output

3 

Input

6 26 5 3 1 2 4

Output

6 

Input

2 100000000002 1

Output

2

Note

Games in the second sample:

3 plays with 1. 3 wins. 1 goes to the end of the line.

3 plays with 2. 3 wins. He wins twice in a row. He becomes the winner.

当k>= n-1时,答案一定是n,剩下的就去模拟一下,不过要注意的是i != 0时,要判断下是否大于前面的。

 1 #include <bits/stdc++.h>
 2 #define ll long long
 3 using namespace std;
 4 const int N = 550;
 5 int a[N];
 6 int main() {
 7     ll n, k;
 8     cin >> n >> k;
 9     for(int i = 0; i < n; i ++) cin >> a[i];
10     if(k >= n) printf("%d\n",n);
11     else {
12         for(int i = 0; i < n; i ++) {
13             int ans = 0;
14             if(i != 0 && a[i] > a[i-1]) ans++;
15             for(int j = 1; j <= n; j ++) {
16                 if(a[i] > a[(i+j)%n]) ans ++;
17                 if(ans >= k) return 0*printf("%d\n",a[i]);
18                 if(a[i] <= a[(i+j)%n]) break;
19             }
20         }
21     }
22     return 0;
23 }

C. Short Program

Petya
learned a new programming language CALPAS. A program in this language
always takes one non-negative integer and returns one non-negative
integer as well.

In the language, there are only three commands:
apply a bitwise operation AND, OR or XOR with a given constant to the
current integer. A program can contain an arbitrary sequence of these
operations with arbitrary constants from 0 to 1023.
When the program is run, all operations are applied (in the given
order) to the argument and in the end the result integer is returned.

Petya
wrote a program in this language, but it turned out to be too long.
Write a program in CALPAS that does the same thing as the Petya‘s
program, and consists of no more than 5 lines. Your program should return the same integer as Petya‘s program for all arguments from 0 to 1023.

Input

The first line contains an integer n (1 ≤ n ≤ 5·105) — the number of lines.

Next n lines contain commands. A command consists of a character that represents the operation ("&", "|" or "^" for AND, OR or XOR respectively), and the constant xi 0 ≤ xi ≤ 1023.

Output

Output an integer k (0 ≤ k ≤ 5) — the length of your program.

Next k lines must contain commands in the same format as in the input.

Examples

Input

3| 3^ 2| 1

Output

2| 3^ 2

Input

3& 1& 3& 5

Output

1& 1

Input

3^ 1^ 2^ 3

Output

0

Note

You can read about bitwise operations in https://en.wikipedia.org/wiki/Bitwise_operation.

Second sample:

Let x be an input of the Petya‘s program. It‘s output is ((x&1)&3)&5 = x&(1&3&5) = x&1. So these two programs always give the same outputs.

所有的操作都可以转换为(((x^a)|b)&c)。  由于范围在0-1023  所以遍历第1位到第9位上的值就行。

x=0表示  每一位上的都是0,y=1023表示每一位上的都是1。

(x&(1<<i)) > 0 表示  x变成了1  (x&(1<<i))  == 0 表示x变成了0。当是y时同理。

所以有四种情况,    每一位都可以表示 (((z^a)|b)&c)

x     y     a b c

0->0  1->0    0 0 0

0->0  1->1    0 0 1

0->1  1->0      1 0 1

0->1  1->1   0 1 1

所以答案就出来了。

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 char str[10];
 4 int main() {
 5     int n, tmp, x = 0, y = 1023;
 6     cin >> n;
 7     for(int i = 1; i <= n; i ++) {
 8         cin >> str >> tmp;
 9         if(str[0] == ‘|‘) {
10             x |= tmp;
11             y |= tmp;
12         } else if(str[0] == ‘^‘) {
13             x ^= tmp;
14             y ^= tmp;
15         } else {
16             x &= tmp;
17             y &= tmp;
18         }
19     }
20     int a = 0, b = 0, c = 0;
21     for(int i = 0; i < 10; i ++) {
22         if((x&(1<<i)) > 0 && (y&(1<<i)) > 0) {
23             b += (1<<i);
24             c += (1<<i);
25         } else if((x&(1<<i)) > 0 && (y&(1<<i)) == 0) {
26             a += (1<<i);
27             c += (1<<i);
28         } else if((x&(1<<i)) == 0 && (y&(1<<i)) > 0) {
29             c += (1<<i);
30         } else {
31
32         }
33     }
34     printf("3\n");
35     printf("^ %d\n", a);
36     printf("| %d\n", b);
37     printf("& %d\n", c);
38     return 0;
39 }

B. Table Tennis

time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

n
people are standing in a line to play table tennis. At first, the first
two players in the line play a game. Then the loser goes to the end of
the line, and the winner plays with the next person from the line, and
so on. They play until someone wins k games in a row. This player becomes the winner.

For
each of the participants, you know the power to play table tennis, and
for all players these values are different. In a game the player with
greater power always wins. Determine who will be the winner.

Input

The first line contains two integers: n and k (2 ≤ n ≤ 500, 2 ≤ k ≤ 1012) — the number of people and the number of wins.

The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ n) — powers of the player. It‘s guaranteed that this line contains a valid permutation, i.e. all ai are distinct.

Output

Output a single integer — power of the winner.

Examples

Input

2 21 2

Output

2 

Input

4 23 1 2 4

Output

3 

Input

6 26 5 3 1 2 4

Output

6 

Input

2 100000000002 1

Output

2

Note

Games in the second sample:

3 plays with 1. 3 wins. 1 goes to the end of the line.

3 plays with 2. 3 wins. He wins twice in a row. He becomes the winner.

时间: 2024-08-30 12:03:09

Codeforces Round #443 (Div. 2)ABC的相关文章

Codeforces Round #247 (Div. 2) ABC

Codeforces Round #247 (Div. 2) http://codeforces.com/contest/431 代码均已投放:https://github.com/illuz/WayToACM/tree/master/CodeForces/431 A - Black Square 题目地址 题意: Jury玩别踩白块,游戏中有四个区域,Jury点每个区域要消耗ai的卡路里,给出踩白块的序列,问要消耗多少卡路里. 分析: 模拟水题.. 代码: /* * Author: illuz

Codeforces Round #366 (Div. 2) ABC

Codeforces Round #366 (Div. 2) A I hate that I love that I hate it水题 1 #I hate that I love that I hate it 2 n = int(raw_input()) 3 s = "" 4 a = ["I hate that ","I love that ", "I hate it","I love it"] 5 fo

Codeforces Round #451 (Div. 2) ABC

A. Rounding Vasya has a non-negative integer n. He wants to round it to nearest integer, which ends up with 0. If n already ends up with 0, Vasya considers it already rounded. For example, if n = 4722 answer is 4720. If n = 5 Vasya can round it to 0 

Codeforces Round #443 Div. 1

A:考虑每一位的改变情况,分为强制变为1.强制变为0.不变.反转四种,得到这个之后and一发or一发xor一发就行了. #include<iostream> #include<cstdio> #include<cmath> #include<cstdlib> #include<cstring> #include<algorithm> using namespace std; #define ll long long #define N

Codeforces Round #312 (Div. 2) ABC题解

[比赛链接]click here~~ A. Lala Land and Apple Trees: [题意]: AMR住在拉拉土地.拉拉土地是一个非常美丽的国家,位于坐标线.拉拉土地是与著名的苹果树越来越随处可见. 拉拉土地恰好n苹果树.树数i位于位置xi和具有人工智能的苹果就可以了增长.阿姆鲁希望从苹果树收集苹果. AMR目前维持在X =0的位置.在开始的时候,他可以选择是否去左边或右边.他会在他的方向继续下去,直到他遇见一棵苹果树,他之前没有参观.他会采取所有的苹果,然后扭转他的方向,继续走这

Codeforces Round #429 (Div. 2)ABC

A: 题意:n个东西,k个朋友,全部给朋友,每个朋友不可以拿同样的,问是否可行 1 #include<bits/stdc++.h> 2 using namespace std; 3 4 map<char ,int >ma; 5 int main(){ 6 int n,k; 7 cin>>n>>k; 8 string s; 9 cin>>s; 10 for(int i=0;i<n;i++){ 11 ma[s[i]]++; 12 if(ma[s

Codeforces Round #450 (Div. 2)ABC

A. Find Extra One You have n distinct points on a plane, none of them lie on OY axis. Check that there is a point after removal of which the remaining points are located on one side of the OY axis. Input The first line contains a single positive inte

Codeforces Round #500 (Div. 2) ABC

A. Piles With Stones 水题略 B. And 题意:让输入一个n,一个下,之后输入一个长为n的数组a,可以在a中使a变为a&x,问经过几次变化数组中有相同的数 思路:当数组中有两个相同的数时直接输出0,注意a&x后的数无论在与x经过几次按位与运算都不会发生变化, 该题有特殊情况,在数据相当大时可能出现错误,因此要ans=min(ans,1或2),在这最后系统wa了,难受emmmmm 知识点补充:只有1&1=1 代码: #include <bits/stdc+

Codeforces Round #262 (Div. 2) 总结:二分

B. Little Dima and Equation 思路:本来前两题都很快做出来的,然后觉得ranting应该可以增加了.然后觉得代码没问题了,又想到了一组cha人的数据,然后就锁了,然后刚锁就被别人cha了看了代码才发现尼玛忘了判断小于10^9了,然后C反正想了好多种方法都不会就没心情了,就这样rating又降了 #pragma comment(linker, "/STACK:1024000000,1024000000") #include<iostream> #in