I - Increasing by Modulo CodeForces - 1169C

Toad Zitz has an array of integers, each integer is between 00 and m−1m−1 inclusive. The integers are a1,a2,…,ana1,a2,…,an.

In one operation Zitz can choose an integer kk and kk indices i1,i2,…,iki1,i2,…,ik such that 1≤i1<i2<…<ik≤n1≤i1<i2<…<ik≤n. He should then change aijaij to ((aij+1)modm)((aij+1)modm) for each chosen integer ijij. The integer mm is fixed for all operations and indices.

Here xmodyxmody denotes the remainder of the division of xx by yy.

Zitz wants to make his array non-decreasing with the minimum number of such operations. Find this minimum number of operations.

Input

The first line contains two integers nn and mm (1≤n,m≤3000001≤n,m≤300000) — the number of integers in the array and the parameter mm.

The next line contains nn space-separated integers a1,a2,…,ana1,a2,…,an (0≤ai<m0≤ai<m) — the given array.

Output

Output one integer: the minimum number of described operations Zitz needs to make his array non-decreasing. If no operations required, print 00.

It is easy to see that with enough operations Zitz can always make his array non-decreasing.

Examples

Input

5 3
0 0 0 1 2

Output

0

Input

5 7
0 6 1 3 2

Output

1

Note

In the first example, the array is already non-decreasing, so the answer is 00.

In the second example, you can choose k=2k=2, i1=2i1=2, i2=5i2=5, the array becomes [0,0,1,3,3][0,0,1,3,3]. It is non-decreasing, so the answer is 11.

#include<bits/stdc++.h>

using namespace std;

int num[300000 + 10];
int n,m;

bool judge(int nn)
{
	int pre = 0;
	for(int i=0; i<n; i++)
	{
//		cout<<"i:"<<i<<endl;
//		cout<<"pre:"<<pre<<endl;
		if(num[i] >= pre)
		{
			if(num[i] + nn >= m && num[i] + nn - m >= pre) continue;//判断num[i]能否变为在nn步内变为pre 
			else pre = num[i];//不能则更新pre 
		}	
		else if(num[i] + nn < pre) return 0;//num[i]小于pre且num[i]在nn步内无法变为pre 
	}	
	return 1;
}

int main()
{	
	cin>>n>>m;
	for(int i=0; i<n; i++)
		scanf("%d", &num[i]);
	int l=0,r=m;
	int ans = 0;
	
//	cout<<"judge(0):"<<judge(0)<<endl;
	while(r>=l)//r>=l否则mid无法等于零 
	{
		int mid = (r + l) / 2;
		if(judge(mid))
		{
			ans = mid;
			r = mid - 1;
//			cout<<r<<endl;
		}							
		else l = mid + 1;
	}
	cout<< ans;
	return 0;
}









 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值