訓練賽20190310

B - Glider

 

A plane is flying at a constant height of hh meters above the ground surface. Let's consider that it is flying from the point (−109,h)(−109,h) to the point (109,h)(109,h) parallel with OxOx axis.

A glider is inside the plane, ready to start his flight at any moment (for the sake of simplicity let's consider that he may start only when the plane's coordinates are integers). After jumping from the plane, he will fly in the same direction as the plane, parallel to OxOx axis, covering a unit of distance every second. Naturally, he will also descend; thus his second coordinate will decrease by one unit every second.

There are ascending air flows on certain segments, each such segment is characterized by two numbers x1x1 and x2x2 (x1<x2x1<x2) representing its endpoints. No two segments share any common points. When the glider is inside one of such segments, he doesn't descend, so his second coordinate stays the same each second. The glider still flies along OxOx axis, covering one unit of distance every second.

 If the glider jumps out at 11, he will stop at 1010. Otherwise, if he jumps out at 22, he will stop at 1212.

Determine the maximum distance along OxOx axis from the point where the glider's flight starts to the point where his flight ends if the glider can choose any integer coordinate to jump from the plane and start his flight. After touching the ground the glider stops altogether, so he cannot glide through an ascending airflow segment if his second coordinate is 00.

Input

The first line contains two integers nn and hh (1≤n≤2⋅105,1≤h≤109)(1≤n≤2⋅105,1≤h≤109) — the number of ascending air flow segments and the altitude at which the plane is flying, respectively.

Each of the next nn lines contains two integers xi1xi1 and xi2xi2 (1≤xi1<xi2≤109)(1≤xi1<xi2≤109) — the endpoints of the ii-th ascending air flow segment. No two segments intersect, and they are given in ascending order.

Output

Print one integer — the maximum distance along OxOx axis that the glider can fly from the point where he jumps off the plane to the point where he lands if he can start his flight at any integer coordinate.

Examples

Input

3 4
2 5
7 9
10 11

Output

10

Input

5 10
5 7
11 12
16 20
25 26
30 33

Output

18

Input

1 1000000000
1 1000000000

Output

1999999999

Note

In the first example if the glider can jump out at (2,4)(2,4), then the landing point is (12,0)(12,0), so the distance is 12−2=1012−2=10.

In the second example the glider can fly from (16,10)(16,10) to (34,0)(34,0), and the distance is 34−16=1834−16=18.

In the third example the glider can fly from (−100,1000000000)(−100,1000000000) to (1999999899,0)(1999999899,0), so the distance is 1999999899−(−100)=199999999

題意:

從飛機上跳傘,正常情況下每秒向右移動一格,向下落一格,當遇到氣流時,不會降落,問最遠可以飛多遠

我們可以想到兩個氣流區域中間的部分其實就是會降落的高度,所以問題就轉化爲了在不超過h的空區域之和的情況下,通過的氣流的長度+高度h。

所以我們可以用前綴和來維護空區域的長度和氣流的長度,在通過lower_bound函數查找從每一個起點(即各個氣流區域的起點)開始的空區域之和小於H的中最長的即可。

#include<bits/stdc++.h>
using namespace std;
const int N=2e5+10;
struct node
{
    int x,y;
}a[N];
int n,h;
int wid[N],kong[N];
int main()
{
    while(~scanf("%d%d",&n,&h)){
            memset(wid,0,sizeof(wid));
            memset(kong,0,sizeof(kong));
        scanf("%d%d",&a[1].x,&a[1].y);
        kong[1]=0;
        wid[1]=a[1].y-a[1].x;
        for(int i=2;i<=n;i++){
            scanf("%d%d",&a[i].x,&a[i].y);
            kong[i]=a[i].x-a[i-1].y;
            kong[i]+=kong[i-1];
            wid[i]=a[i].y-a[i].x;
            wid[i]+=wid[i-1];
        }
          /*for(int i=1;i<=n;i++)
            cout<<wid[i]<<endl;*/
        int ans=-1;
        for(int i=1;i<=n;i++){
            int p=lower_bound(kong+1,kong+1+n,kong[i]+h)-kong;
           // cout<<p<<" "<<kong[p]<<endl;
             ans=max(ans,wid[p-1]-wid[i-1]+h);
        }
        printf("%d\n",ans);
    }
}

C - Bacteria

 

Recently Monocarp has created his own mini-laboratory!

The laboratory contains nn bacteria. Monocarp knows that he can merge any two bacteria having equal sizes, and the resulting bacterium will have the size equal to the sum of sizes of merged bacteria. For example, if two bacteria having sizes equal to 77 merge, one bacterium with size 1414 is the result.

It becomes hard to watch for many bacteria, so Monocarp wants to merge all of them into one bacterium. It may not be possible to do this with the bacteria Monocarp has, so he can buy any number of bacteria of any possible integer sizes in a special store.

You have to determine the minimum number of bacteria Monocarp has to buy to merge them with the nn bacteria his laboratory contains into exactly one bacterium.

Input

The first line contains one integer nn (1≤n≤2⋅105)(1≤n≤2⋅105) — the number of bacteria Monocarp's laboratory contains.

The second line contains nn integers a1,a2,…,ana1,a2,…,an (1≤ai≤109)(1≤ai≤109), where aiai is the size of the ii-th bacterium in the laboratory.

Output

If it is impossible to merge the bacteria (possibly after buying some) into only one bacterium, print -1.

Otherwise print the minimum number of bacteria Monocarp has to buy to merge them with the nn bacteria his laboratory contains into exactly one bacterium.

Examples

Input

2
1 4

Output

2

Input

3
3 6 9

Output

-1

Input

7
1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000

Output

1

Note

In the first example Monocarp should buy one bacterium having size 11 and one bacterium having size 22. Then Monocarp will have 44 bacteria having sizes [1,4,1,2][1,4,1,2]. Then two bacteria having sizes 11 can be merged into one having size 22. Then Monocarp will have 33 bacteria having sizes [2,4,2][2,4,2]. Then two bacteria having sizes 22 can be merged into one having size 44. Then Monocarp will have 22 bacteria having sizes [4,4][4,4], which can be merged into one having size 88.

In the second example no matter which bacteria Monocarp will buy, he cannot merge all his bacteria.

In the third example Monocarp needs to buy one bacterium having size 10000000001000000000.

題意:兩個同樣大小的細菌可以合併,如果沒有一樣大的,需要買一個。。。直到合併爲一個,問至少需要買幾個,若始終不能合併成一個,輸出-1

很明顯,不能合併成一個的條件就是最後剩下的兩個不相等並且當小的那個數乘2大於另一個數,這樣是無論如何都不能合併成一個的;

這裏用優先隊列維護即可。

#include<bits/stdc++.h>
using namespace std;
int n;
priority_queue<long long,vector<long long>,greater<long long> >q;
long long gcd(long long m,long long n)
{
   while(m>0)
   {
     long long c=n%m;
         n=m;
         m=c;
    }
return n;
}
int main()
{
    while(~scanf("%d",&n)){
        int x;
        while(!q.empty())
            q.pop();
        for(int i=1;i<=n;i++){
            scanf("%d",&x);
            q.push(x);
        }
        int ans=0;
        bool flag=true;
        while(!q.empty()){
            long long p1=q.top();
            q.pop();
            long long p2=q.top();
            q.pop();
            if(q.empty()){
                if(p1!=p2&&p1*2>p2){
                        flag=false;
                      break;
                }
            }
            if(p1==p2){
                p1+=p2;
                q.push(p1);
            }
            else{
                p1=p1*2;
                ans++;
                q.push(p2);
                q.push(p1);
            }
        }
        if(flag)
            printf("%d\n",ans);
        else printf("-1\n");
    }
}

H - Theater Square

The Theater Square can be represented as a rectangle having height nn and length mm, divided into square 1×11×1 cells. Let's denote the cell located at the intersection of ii-th row and jj-th column as (i,j)(i,j). The rows are numbered from top to bottom, the columns — from left to right.

There is a rectangular fountain inside the Teather Square. The cell in its left upper corner is (x1,y1)(x1,y1), the cell in its right lower corner is (x2,y2)(x2,y2).

The Theater Square soon will be paved with tiles having height 11 and length 22. Every cell (except cells inside the fountain) should be paved, and no cell should be covered by more than one tile. All tiles will be laid out horizontally, so the cells covered by each tile are in the same row. To pave the whole Theater Square it might be necessary to break some tiles. After breaking a tile, two new tiles of size 1×11×1are formed (which cannot be broken further). You may consider that the mayor, who ordered the paving of the Theater Square, has infinite number of tiles 1×21×2.

Since broken tiles are not beautiful, among all possible ways to pave the Theater Square the mayor wants to choose a way such that the number of tiles to be broken into two lesser tiles is minimum possible. Pay attention that tiles should be laid horizontally, no tile can cover cells in different rows.

Help the mayor! Tell him the minimum possible number of tiles to be broken.

Input

The first line contains two integers nn and mm (1≤n,m≤2⋅105)(1≤n,m≤2⋅105) — the height and the length of the Theater Square, respectively.

The second line contains four numbers x1,y1,x2,y2 (1≤x1≤x2≤n,1≤y1≤y2≤m)x1,y1,x2,y2 (1≤x1≤x2≤n,1≤y1≤y2≤m) — the coordinates of left upper corner and right lower corner of the fountain.

Output

Print one number — minimum possible number of tiles mayor has to break in order to pave the whole Theater Square.

Examples

Input

6 5
1 2 3 4

Output

5

Input

6 1
3 1 4 1

Output

2

Input

1 12
1 3 1 8

Output

0

Note

One of the optimal ways to pave the Theater Square in the first example:

55 tiles are to be broken.

題意:有一個n×m的廣場,需要鋪設1×2的磚,廣場裏還有一個噴泉,1×2的磚可以分成1×1的磚,問至少需要幾塊1×2的磚分成1×1的磚?

,這個題可以先求需要的1×1的磚的數量,最後除2向上取整即可,1×1的磚按照噴泉的左右剩下的列數判斷即可,奇數就需要一列,另外噴泉沒有佔到的行也判斷一下,列數如果是奇數,也需要一列。

#include<bits/stdc++.h>
using namespace std;
int n,m,ans;
int main()
{
    int x1,x2,y1,y2;
    while(~scanf("%d%d",&n,&m)){
            ans=0;
        scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
        if((y1-1)&1){
            ans+=x2-x1+1;
        }
        if((m-y2)&1){
            ans+=x2-x1+1;
        }
        if(m&1){
            ans+=x1-1+n-x2;
        }
        printf("%d\n",ans%2==0?ans/2:(ans/2)+1);
    }
}

I - Heist

There was an electronic store heist last night.

All keyboards which were in the store yesterday were numbered in ascending order from some integer number xx. For example, if x=4x=4 and there were 33 keyboards in the store, then the devices had indices 44, 55 and 66, and if x=10x=10 and there were 77 of them then the keyboards had indices 1010, 1111, 1212, 1313, 1414, 1515 and 1616.

After the heist, only nn keyboards remain, and they have indices a1,a2,…,ana1,a2,…,an. Calculate the minimum possible number of keyboards that have been stolen. The staff remember neither xx nor the number of keyboards in the store before the heist.

Input

The first line contains single integer nn (1≤n≤1000)(1≤n≤1000) — the number of keyboards in the store that remained after the heist.

The second line contains nn distinct integers a1,a2,…,ana1,a2,…,an (1≤ai≤109)(1≤ai≤109) — the indices of the remaining keyboards. The integers aiai are given in arbitrary order and are pairwise distinct.

Output

Print the minimum possible number of keyboards that have been stolen if the staff remember neither xx nor the number of keyboards in the store before the heist.

Examples

Input

4
10 13 12 8

Output

2

Input

5
7 5 6 4 8

Output

0

Note

In the first example, if x=8x=8 then minimum number of stolen keyboards is equal to 22. The keyboards with indices 99 and 1111 were stolen during the heist.

In the second example, if x=4x=4 then nothing was stolen during the heist.

題意:商店裏有下標遞增的鍵盤,突然商店被盜,有一些鍵盤丟失,問最少丟了多少個鍵盤

水題,找出剩下的鍵盤中下標最大的和最小的,max-min+1即可求出個數,在減去n即可。

#include <iostream>
#include<cstdio>
#include<cmath>
#include<algorithm>
#include<map>
#include<queue>
#include<cstdlib>
using namespace std;
#define ios   ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);'
#define ll long long
const int maxn=1000005;
int gcd(int x,int y){return y==0?x:gcd(y,x%y);}
int n;
int main()
{
    while(~scanf("%d",&n)){
        int minx,maxn;
        minx=0x3f3f3f3f;
        maxn=-1;
        int m;
        for(int i=1;i<=n;i++){
            scanf("%d",&m);
            if(m>maxn)
                maxn=m;
            if(m<minx)
                minx=m;
        }
        printf("%d\n",maxn-minx+1-n);
    }
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章