Problem:
最長の階段
問題
1 から
n
までの整数がそれぞれ 1 つずつ書かれた
n
枚のカードと,1 枚の白紙の
カードがある.これら
n
+ 1 枚のカードの内,
k
枚のカードが与えられる.ただし,
1 ≤
k
≤ n である.白紙のカードには 1 から
n
までの整数を 1 つ書くことができ
る.与えられたカードだけで,できるだけ長い連続した整数列を作りたい.
与えられるカードが入力されたときに, 与えられたカードから作ることができる連
続した整数列の最大長を出力するプログラムを作成せよ.
例1
n
= 7,
k
= 5 とする.6, 2, 4, 7, 1 のカードが与えられたとき,このカードを使っ
て作れる連続した整数列のうち最長のものは 1, 2 であり,その長さは 2 である.
例2
n
= 7,
k
= 5 とする.6, 2, 4, 7 と白紙のカードが与えられたとき,このカードを
使って作れる連続した整数列のうち最長のものは,白紙のカードに 5 を書くことに
よってできる 4, 5, 6, 7 であり,その長さは 4 である.
入力
入力は複数のデータセットからなる．各データセットは以下の形式で与えられる．入力はゼロ２つを含む行で終了する．
1 行目には,2 つの整数
n
(1 ≤
n
≤ 100000) と
k
(1 ≤
k
≤
n
) がこの順で 1 つの
空白を区切りとして書かれている.続く
k
行には整数が 1 つずつ書かれており,与
えられる
k
枚のカードに書かれている整数を表している.白紙のカードは 0 で表さ
れる.
採点用データのうち, 配点の 40% 分は 1 ≤
n
≤ 1000, 1 ≤
k
≤ 500 を, 配点の 20% 分は 1 ≤
n
≤ 60000, 1 ≤
k
≤  50000 を. 配点の 40% 分は 1 ≤
n
≤  100000, 1 ≤
k
≤ 100000 を満たす.
データセットの数は 5 を超えない．
出力
データセットごとに整数を1行に出力する．
入出力例
入力例
7 5
6
2
4
7
1
7 5
6
2
0
4
7
0 0
出力例
2
4
上記問題文と自動審判に使われるデータは、
情報オリンピック日本委員会
が作成し公開している問題文と採点用テストデータです。

Submitted solutions:

code_snippet: #include <iostream>
#include <math.h>
#include <vector>
#include <algorithm>
#include <set>
using namespace std;
using ll = long long;
int main(){
    while(true){
        int N,K;
        cin >> N >> K;
        if(N == 0 and K == 0) break;
        int zero = 0;
        vector<int> v(N+1,false);
        vector<vector<int>> vv(2,vector<int>(N+1,0));
        for(int i = 0;i < K;i++){
            int c;
            cin >> c; 
            if(c == 0) zero = 1;
            v[c] = true;
        }
        int ans = 0;
        for(int i = 1;i < N+1;i++){
            vv[1][i] = (vv[0][i-1] + 1)*zero;
            if(v[i]){
                vv[0][i] = vv[0][i-1] + 1;
                vv[1][i] = (vv[1][i-1] + 1)*zero;
                if(ans < vv[0][i]) ans = vv[0][i];
                if(ans < vv[1][i]) ans = vv[1][i];
            }
        }
        cout << ans << endl;
    }
}
accuracy: 1
time_ms: 10
memory_kb: 4660
final_rank: 16

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

using ll = long long;

int main() {
    int n, k;
    while(cin >> n >> k, n) {
        vector<int> v(k);
        for(int i=0; i<k; ++i) {
            cin >> v[i];
        }
        sort(v.begin(), v.end());
        int f = v[0] == 0;
        int l = 0+f, r = 1+f, cnt = 0, next_l = -1;
        int res = 0;
        while(true) {
            next_l = -1;
            while(r < k && (v[r] - v[r-1] == 1 || v[r] - v[r-1] == 2)) {
                if(v[r] - v[r-1] == 1) {
                    r++;
                } else if(cnt < f) {
                    cnt++;
                    next_l = r;
                    r++;
                } else {
                    break;
                }
            }
            res = max(res, r - l + ((r == k && cnt < f) || cnt));
            if(r == k) {
                break;
            }
            if(next_l == -1) {
                l = r;
                r++;
            } else {
                l = next_l;
            }
            cnt = 0;
        }
        cout << res << endl;
    }
}
accuracy: 1
time_ms: 10
memory_kb: 3376
final_rank: 4

code_snippet: #include <iostream>
#include <vector>
#include<algorithm>
using namespace std;

int main() {
    int N, K;
    while (true) {
        cin >> N >> K;
        if (N == 0 and K == 0) {
            break;
        }
        vector<int>A(N, 0);
        int c = -1;
        for (int i = 0; i < K; i++) {
            int x;
            cin >> x;

            if (x > 0) {
                A[x - 1] = 1;
            }
            else if (x == 0) {
                c = 0;
            }
        }
        int max_ans = 0;
        int skipsiyou = 0;
        int jun = 0;
        for (int i = 0; i < N; i++) {
            if (c != -1) {
                if (A[i] == 1) {
                    jun += 1;
                    skipsiyou += 1;
                }
                else if (A[i] == 0) {
                    max_ans = max(max_ans, skipsiyou);
                    skipsiyou = jun + 1;
                    jun = 0;
                }
            }
            else {
                if (A[i] == 1) {
                    jun += 1;
                }
                else if (A[i] == 0) {
                    max_ans = max(jun, max_ans);
                    jun = 0;
                }
            }
        }
        max_ans = max(max_ans, skipsiyou);
        cout << max_ans << endl;
    }
    return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 3516
final_rank: 6

code_snippet: #include <bits/stdc++.h>
using namespace std;
#define ll long long    
#define INF 1e18
int main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);

    while(1){
        ll ans=0,n,k;cin>>n>>k;
        if(n==0)return 0;
        vector<pair<ll,ll>> dp(n+10);
        map<ll,bool> ma;
        while(k--){
            ll a;cin>>a;
            ma[a]=1;
        }
        for(ll i=1;i<=n;i++){
            if(!ma[0]){
                if(ma[i])dp[i+1].first=dp[i].first+1;
            }else{
                if(ma[i]){
                    dp[i+1].first=dp[i].first+1;
                    dp[i+1].second=dp[i].second+1;
                }
                else{
                    dp[i+1].second=dp[i].first+1;
                }
            }
            ans=max({ans,dp[i+1].first,dp[i+1].second});
        }
        cout<<ans<<endl;
    }
}
accuracy: 1
time_ms: 20
memory_kb: 11032
final_rank: 19

code_snippet: #include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); ++i)

struct pair_hash {
    template <typename T1, typename T2>
    size_t operator ()(const pair<T1, T2>& p) const {
        auto h1 = hash<T1>{}(p.first);
        auto h2 = hash<T2>{}(p.second);
        return h1 ^ (h2 << 1);
    }
};

int main(){
    while(1){
        int n,k;
        cin >> n >> k;
        if(n==0 && k==0) break;

        vector<bool> present(n+1,false);
        rep(i,k){
            int x;
            cin >> x;
            present[x]=true;
        }
        int allowed=0;
        if(present[0]){
            allowed=1;
        }

        int res=0;
        int left=1;
        int gap=0;
        for(int right=1;right<=n;right++){
            if(!present[right]){
                gap++;
            }
            while(gap>allowed && left<=right){
                if(!present[left]){
                    gap--;
                }
                left++;
            }
            res=max(res,right-left+1);
        }
        cout << res << endl;
    }
    return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 3224
final_rank: 3

code_snippet: #include <bits/stdc++.h>
using namespace std;
#ifdef LOCAL
  #include "settings/debug.cpp"
#else
  #define Debug(...) void(0)
#endif
#define rep(i, n) for (int i = 0; i < (n); ++i)
using ll = long long;
using ull = unsigned long long;

int main() {
  while (true) {
    int n, k;
    cin >> n >> k;
    if (n + k == 0) break;
    vector<int> a(k);
    rep(i, k) cin >> a[i];
    sort(a.begin(), a.end());
    bool has0 = a[0] == 0;
    if (has0) {
      a.erase(a.begin());
      k--;
    }
    vector<pair<int, int>> v(0);
    for (int x : a) {
      if (v.empty() || v.back().second + 1 < x) {
        v.push_back({ x, x });
      }
      else {
        assert(v.back().second + 1 == x);
        v.back().second = x;
      }
    }
    Debug(v);
    int ans = 0;
    rep(i, v.size()) {
      if (has0 && i + 1 < v.size() && v[i].second + 2 == v[i + 1].first) {
        ans = max(ans, v[i + 1].second - v[i].first + 1);
      }
      else {
        ans = max(ans, v[i].second - v[i].first + 1 + has0);
      }
    }
    cout << ans << endl;
  }
  return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 3968
final_rank: 14

code_snippet: #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
#define rep(i, n) for (int i=0; i<n; ++i)
#define repi(i, j, n) for (int i=j; i<n; ++i)

int main () {
	rep(t, 5) {
		int n, k, x;
		cin >> n >> k;
		if (n == 0 && k == 0) return 0;
		vector<bool> v(n+1, false);
		rep(i, k) {
			cin >> x;
			v[x] = true;
		}

		int ans = 0, right = 1;
		bool bln = v[0];
		repi(left, 1, n) {
			while (right <= n) {
				if (v[right]) ++right;
				else if (v[0] && bln) {
					bln = false;
					++right;
				}
				else break;
			}
			
			ans = max(ans, right - left);
			if (left == right) ++right;
			if (!v[left] && v[0]) bln = true;
		}
		printf("%d\n", ans);
	}
	return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 3432
final_rank: 5

code_snippet: #include<bits/extc++.h>
using namespace std;

#define ll long long
#define ld long double
#define ull unsigned long long
#define _overload4(_1,_2,_3,_4,name,...) name
#define _overload3(_1,_2,_3,name,...) name
#define _rep1(n) for(ll i = 0; i < n; ++i)
#define _rep2(i, n) for(ll i = 0; i < n; ++i)
#define _rep3(i, a, b) for(ll i = a; i < b; ++i)
#define _rep4(i, a, b, c) for(ll i = a; i < b; i += c)
#define rep(...) _overload4(__VA_ARGS__, _rep4, _rep3, _rep2, _rep1)(__VA_ARGS__)
#define rrep(i, n) for(int i = n-1; i >= 0; i--)
#define rrep2(i, a, b) for(int i = a-1; i >= b; i--)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define MAX(x) *max_element(all(x))
#define MIN(x) *min_element(all(x))
#define eb emplace_back
#define fi first
#define se second
#define pii pair<int, int>
#define pll pair<ll, ll>
#define pcc pair<char, char>
#define pdd pair<double, double>
#define endl '\n'
template<class T> using pq = priority_queue< T >;
template<class T> using pqg = priority_queue< T , vector< T >, greater< T >>;
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }
template<class T>
T ceil_div(T a, T b) {
    if(b < 0) a = -a, b = -b;
    return (a >= 0 ? (a + b - 1) / b : a / b);
}
 
template<class T>
T floor_div(T a, T b) {
    if(b < 0) a = -a, b = -b;
    return (a >= 0 ? a / b : (a - b + 1) / b);
}
inline ll intpow(ll a, ll b){ ll ans = 1; while(b){ if(b & 1) ans *= a; a *= a; b /= 2; } return ans; }
ll digit(ll x){
  string s = to_string(x);
  return (ll)s.size();
}

const int inf = 1001001001;
const ll INF = 1001001001001001001;

const double PI = acos(-1);

bool range(int y, int x, int h, int w){
  return (0 <= y && y < h && 0 <= x && x < w);
}
int dy[8] = {0, 1, 0, -1, 1, 1, -1, -1};
int dx[8] = {1, 0, -1, 0, 1, -1, -1, 1};

const int MOD = 998244353;
//const int MOD = 1000000007;
struct dsu {
  public:
    dsu() : _n(0) {}
    explicit dsu(int n) : _n(n), parent_or_size(n, -1) {}

    int merge(int a, int b) {
        assert(0 <= a && a < _n);
        assert(0 <= b && b < _n);
        int x = leader(a), y = leader(b);
        if (x == y) return x;
        if (-parent_or_size[x] < -parent_or_size[y]) std::swap(x, y);
        parent_or_size[x] += parent_or_size[y];
        parent_or_size[y] = x;
        return x;
    }

    bool same(int a, int b) {
        assert(0 <= a && a < _n);
        assert(0 <= b && b < _n);
        return leader(a) == leader(b);
    }

    int leader(int a) {
        assert(0 <= a && a < _n);
        if (parent_or_size[a] < 0) return a;
        return parent_or_size[a] = leader(parent_or_size[a]);
    }

    int size(int a) {
        assert(0 <= a && a < _n);
        return -parent_or_size[leader(a)];
    }

    std::vector<std::vector<int>> groups() {
        std::vector<int> leader_buf(_n), group_size(_n);
        for (int i = 0; i < _n; i++) {
            leader_buf[i] = leader(i);
            group_size[leader_buf[i]]++;
        }
        std::vector<std::vector<int>> result(_n);
        for (int i = 0; i < _n; i++) {
            result[i].reserve(group_size[i]);
        }
        for (int i = 0; i < _n; i++) {
            result[leader_buf[i]].push_back(i);
        }
        result.erase(
            std::remove_if(result.begin(), result.end(),
                           [&](const std::vector<int>& v) { return v.empty(); }),
            result.end());
        return result;
    }

  private:
    int _n;
    // root node: -1 * component size
    // otherwise: parent
    std::vector<int> parent_or_size;
};
signed main(){
  cout << fixed << setprecision(15);
  
  while(true){
    int n, k;
    cin >> n >> k;
    if(n == 0 && k == 0) break;
    vector<int> x(100010);
    dsu tree(100010);
    bool zero = false;
    rep(i, k){
      int a;
      cin >> a;
      if(a == 0) zero = true;
      else x[a] = 1;
    }
    rep(i, 1, 100000){
      if(x[i] == 1 && x[i+1] == 1) tree.merge(i, i+1);
    }
    int ans = 0;
    rep(i, 1, 100001){
      chmax(ans, (int)tree.size(i));
      if(zero){
        if(x[i] == 1 && x[i+1] == 0 && x[i+2] == 1){
          chmax(ans, (int)tree.size(i) + 1 + (int)tree.size(i+2));
        }
      }
    }
    cout << ans << endl;
  }
  return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 3592
final_rank: 10

code_snippet: #include <bits/stdc++.h>
using namespace std;
const long long MOD1 = 1000000007;
const long long MOD2 = 998244353;
typedef long long ll;
typedef pair<long long, long long> P;
const long long INF = 1e9;
template <typename T>
void input_arr(vector<T>& A, long long N) {
    for (long long i = 0; i < N; i++) {
        cin >> A[i];
    }
}
ll binary_search(vector<long long>&A, long long key) {
    ll left = (long long)A.size(), right = -1;
    if (A.back() < key) return INF;
    while (abs(left-right)>1){
        long long mid = (left + right) / 2;
        if (A[mid] > key) left = mid;
        else right = mid;
    }
    return left;
}
int main() {
    long long N, K; cin >> N >> K;
    while (!(N == 0 && K == 0)) {
        vector<long long>A(K); input_arr(A, K);
        sort(A.begin(),A.end());
        vector<bool>B(N, false);
        if (A[0] == 0) {
            for (long long i = 1; i < K; i++) {
                B[A[i]-1] = true;
            }
            vector<P>C;
            C.push_back(make_pair(1, B[0]));
            for (long long i = 1; i < N; i++) {
                if (B[i] == B[i-1]) C.back().first++;
                else C.push_back(make_pair(1, B[i]));
            }
            long long ans = -1;
            for (long long i = 0; i < C.size(); i++) {
                if (C[i].second == 0 && C[i].first == 1) {
                    long long sum = 0;
                    if (i - 1 >= 0) sum += C[i-1].first;
                    if (i + 1 < C.size()) sum += C[i+1].first;
                    sum++;
                    ans = max(ans, sum);
                }
                else if (C[i].second == 1) ans = max(ans, C[i].first);
            }
            cout << ans << endl;
        } 
        else {
            for (long long i = 0; i < K; i++) {
                B[A[i]-1] = true;
            }
            vector<P>C;
            C.push_back(make_pair(1, B[0]));
            for (long long i = 1; i < N; i++) {
                if (B[i] == B[i-1]) C.back().first++;
                else C.push_back(make_pair(1, B[i]));
            }
            long long ans = -1;
            for (long long i = 0; i < C.size(); i++) {
                if (C[i].second == 1) ans = max(ans, C[i].first);
            }
            cout << ans << endl;
        }
        cin >> N >> K;
    }
}
accuracy: 1
time_ms: 10
memory_kb: 4296
final_rank: 15

code_snippet: #include<iostream>
#include<set>
#include<map>

using namespace std;

int main()
{
	while (true)
	{
		int n, k;
		cin >> n >> k;
		if (n == 0 && k == 0)
			break;
		set<int> nums;
		map<int, int> counter;
		int num;
		for (int i = 0; i < k; i++)
		{
			cin >> num;
			nums.insert(num);
		}
		bool haveZero = false;
		auto iter = nums.begin();
		if (*iter == 0)
		{
			haveZero = true;
			iter = nums.erase(iter);
		}
		auto iter_rear = nums.begin();
		int ans = 0;
		while (iter != nums.end())
		{
			counter[*iter] = 0;
			for (iter_rear = iter; iter_rear != nums.end(); iter_rear++)
			{
				if (*iter_rear - *iter != counter[*iter])
					break;
				counter[*iter]++;
			}
			ans = max(ans, counter[*iter]);
			iter = iter_rear;
		}
		if (haveZero)
		{
			for (pair<int,int> c : counter)
			{
				if (counter.count(c.first + c.second + 1))
					ans = max(ans, counter[c.first] + counter[c.first + c.second + 1] + 1);
			}
		}
		cout << ans << endl;
	}

	//system("pause");
}
accuracy: 1
time_ms: 40
memory_kb: 7492
final_rank: 20

code_snippet: #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, N) for (ll i = 0; i < N; i++)

int main()
{

  while(1){
  int n, k;
  cin >> n >> k;
  if(n ==0 && k == 0) return 0;
  int kk[k], nn[n + 2] = {};
  rep(i, k)
  {
    cin >> kk[i];
    nn[kk[i]]++;
  }
  int maxv = 0;
  int cnt = 0;
  int prevs = -10, preve = -10, curs = -10, prevcnt = -10;
  if (nn[0] == 1)
  {
    for (int i = 1; i <= n + 1; i++)
    {
      if (nn[i] == 1)
      {
        cnt++;
        if (curs < 0)
          curs = i;
      }
      else
      {
        if (nn[i - 1] == 1)
        {
          int tmp = cnt;
          if (curs - 2 == preve)
          {
            cnt += prevcnt + 1;
          }
          else
            cnt++;
          if (maxv < cnt)
          {
            maxv = cnt;
          }
          prevcnt = tmp;
          prevs = curs;
          curs = -1;
          preve = i - 1;
        }
        else
        {
          prevcnt = 0;
        }
        cnt = 0;
      }
   //   cout << i << " " << prevs << " " << preve << " " << curs << " " << prevcnt << " " << cnt << " " << maxv << endl;
    }
  }
  else
  {
    for (int i = 1; i <= n; i++)
    {
      if (nn[i] == 1)
      {
        cnt++;
      }
      else
      {
        if (maxv < cnt)
        {
          maxv = cnt;
        }
        cnt = 0;
      }
  //    cout << i << " " << prevs << " " << preve << " " << curs << " " << prevcnt << " " << cnt << " " << maxv << endl;
    }
  }
  cout << maxv << endl;
  }
}
accuracy: 1
time_ms: 10
memory_kb: 3772
final_rank: 11

code_snippet: #include <bits/stdc++.h>
 
#define rep(i,n) for(ll i=0;i<n;++i)
#define rrep(i, n) for(ll i=n-1;i>=0;--i)
#define rep1(i, n) for(ll i=1; i<=n; ++i)
#define repitr(itr,mp) for(auto itr=mp.begin();itr!=mp.end();++itr)
#define ALL(a) (a).begin(),(a).end()

template<class T> void chmax(T &a, const T &b){if(a < b){a = b;}}
template<class T> void chmin(T &a, const T &b){if(a > b){a = b;}}

using namespace std;
using ll  = long long;
using ld = long double;
using ull = unsigned long long;
using pll = pair<ll, ll>;

const ll MOD  = 1e9 + 7;
const ll LINF = 1LL << 60;
const int INF = 1e9 + 7;
const double PI = 3.1415926535897932384626;

int main(){
    ll n, k;
    while(cin >> n >> k){
        if(n + k == 0)break;

        vector<ll> num(n+1, 0);
        rep(i, k){
            ll x;
            cin >> x;
            num[x] = 1;
        }

        ll back = 0;
        ll ans = 0;
        //　白紙がない場合
        if(!num[0]){
            rep1(i, n){
                if(!num[i])back = i;
                else chmax(ans, i - back);
            }
        }
        // 白紙がある場合
        else {
            int zero = 0;
            rep1(i, n){
                if(!num[i]){
                    chmax(ans, zero - back);
                    back = zero;
                    zero = i;
                }
                else chmax(ans, i - back);
            }
        }

        cout << ans << endl;
    }
}
accuracy: 1
time_ms: 10
memory_kb: 3580
final_rank: 9

code_snippet: #include <bits/stdc++.h>
 
#define rep(i,n) for(ll i=0;i<n;++i)
#define rrep(i, n) for(ll i=n-1;i>=0;--i)
#define rep1(i, n) for(ll i=1; i<=n; ++i)
#define repitr(itr,mp) for(auto itr=mp.begin();itr!=mp.end();++itr)
#define ALL(a) (a).begin(),(a).end()

template<class T> void chmax(T &a, const T &b){if(a < b){a = b;}}
template<class T> void chmin(T &a, const T &b){if(a > b){a = b;}}

using namespace std;
using ll  = long long;
using ld = long double;
using ull = unsigned long long;
using pll = pair<ll, ll>;

const ll MOD  = 1e9 + 7;
const ll LINF = 1LL << 60;
const int INF = 1e9 + 7;
const double PI = 3.1415926535897932384626;

int main(){
    ll n, k;
    while(cin >> n >> k){
        if(n + k == 0)break;

        vector<ll> num(k);
        rep(i, k)cin >> num[i];
        sort(ALL(num));

        bool has0 = !num[0];
        if(has0){
            num.erase(num.begin());
        }
        // NOTE: *_usingは白紙カードをつかった場合のやつ
        vector<ll> ans(1, num[0]), ans_using(1, num[0]), tmp(1, num[0]), tmp_using(1, num[0]);
        rep1(i, num.size()-1){
            if(tmp.empty() || num[i-1] + 1 == num[i]){
                tmp.push_back(num[i]);
                tmp_using.push_back(num[i]);
            }
            else {
                if(tmp.size() > ans.size())ans = tmp;
                if(tmp_using.size() > ans_using.size())ans_using = tmp_using;

                // 白紙を使う
                if(num[i-1] + 2 == num[i]){
                    tmp_using = tmp;
                    tmp_using.push_back(num[i]);
                }
                else {
                    tmp_using.clear();
                    tmp_using.push_back(num[i]);
                }

                tmp.clear();
                tmp.push_back(num[i]);
            }
        }

        if(tmp.size() > ans.size())ans = tmp;
        if(tmp_using.size() > ans_using.size())ans_using = tmp_using;

        ll res;
        if(has0)res = max(ans.size(), ans_using.size()) + 1;
        else res = ans.size();

        cout << min(res, k) << endl;
    }
}
accuracy: 1
time_ms: 20
memory_kb: 3324
final_rank: 18

code_snippet: #include <bits/stdc++.h>
//#include <atcoder/all>
#define LOCAL
using namespace std;
//using namespace atcoder;
template <typename A, typename B>
ostream& operator <<(ostream& out, const pair<A, B>& a) {
out << "(" << a.first << "," << a.second << ")";
return out;
}
template <typename T, size_t N>
ostream& operator <<(ostream& out, const array<T, N>& a) {
out << "["; bool first = true;
for (auto& v : a) { out << (first ? "" : ", "); out << v; first = 0;} out << "]";
return out;
}
template <typename T>
ostream& operator <<(ostream& out, const vector<T>& a) {
out << "["; bool first = true;
for (auto& v : a) { out << (first ? "" : ", "); out << v; first = 0;} out << "]";
return out;
}
template <typename T, class Cmp>
ostream& operator <<(ostream& out, const set<T, Cmp>& a) {
out << "{"; bool first = true;
for (auto& v : a) { out << (first ? "" : ", "); out << v; first = 0;} out << "}";
return out;
}
template <typename U, typename T, class Cmp>
ostream& operator <<(ostream& out, const map<U, T, Cmp>& a) {
out << "{"; bool first = true;
for (auto& p : a) { out << (first ? "" : ", "); out << p.first << ":" << p.second; first = 0;} out << "}";
return out;
}
#ifdef LOCAL
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
#else
#define trace(...) 42
#endif
template <typename Arg1>
void __f(const char* name, Arg1&& arg1){
cerr << name << ": " << arg1 << endl;
}
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args){
const char* comma = strchr(names + 1, ',');
cerr.write(names, comma - names) << ": " << arg1 << " |";
__f(comma + 1, args...);
}
#define rep(i,n) for(int i=0; i<(n); i++)
using ll = long long;
//#define int long long
using P = pair<int,int>;
//#########################################
int main(){
    while(1){
        int n,k;cin >>n >> k;
        if(n == 0 && k == 0) return 0;
        vector<int> a;
        int flag = 0;
        rep(i,k){
            int x;cin >> x;
            if(x == 0){
                flag = 1;
                continue;
            }
            a.push_back(x);
        }
        sort(a.begin(),a.end());
        int m = a.size();
        vector<int> kouho;
        rep(i,m){
            if(i == m-1 || a[i] +1 != a[i+1])kouho.push_back(1);
            else{
                int cnt = 0;
                while(i < m-1 && a[i] +1 == a[i+1]){
                    cnt++;
                    i++;
                }
                kouho.push_back(cnt+1);
                
            }

        }
        // trace(kouho);
        m = kouho.size();
        if(flag){
            int ret = 0;
            for(int i = 0;i < m-1;i++){
                ret = max(ret,kouho[i] + kouho[i+1]);
            }
            cout << ret +1 << endl;
        }
        else {
            int ret = 0;
            rep(i,m)ret = max(ret,kouho[i]);
            cout << ret << endl;
        }
     }
    return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 3556
final_rank: 7

code_snippet: #include<iostream>
#include<vector>
#include<functional>
#include<algorithm>
using namespace std;

template<typename T>
T meguru_search(T ok, T ng, function<bool(T)> check){
    while(max(ok, ng) - min(ok, ng) > T(1)){
        T mid = (ok + ng) / T(2);
        if(check(mid)){
            ok = mid;
        }else{
            ng = mid;
        }
    }
    return ok;
}

int main(){
    int n, k;

    while(cin >> n >> k, n){
        vector<int> sum(n + 1, 0);
        int cnt = 0;

        for(int i = 0 ; i < k ; ++i){
            int a;
            cin >> a;

            if(a == 0){
                ++cnt;
            }else{
                ++sum[a];
            }
        }

        for(int i = 1 ; i <= n ; ++i){
            sum[i] += sum[i - 1];
        }

        auto check = [&](int mid){
            for(int i = mid ; i <= n ; ++i){
                if(sum[i] - sum[i - mid] + cnt >= mid){
                    return true;
                }
            }
            return false;
        };

        int ans = meguru_search<int>(0, n + 1, check);
        cout << ans << endl;
    }

    return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 3064
final_rank: 2

code_snippet: #include<iostream>
#include<vector>
#include<algorithm>
#include<queue>
#include<iomanip>
#include <string>
#include <math.h>
using namespace std;

int main(){
    int n,k,q,w,ans;
    cin>>n;
    vector<int>a(200000);
    vector<int>b(200000);
    while(n!=0){
        cin>>k;
        q=0;
        w=0;
        ans=0;
        vector<int>b(200000,0);
        for(int i=0;i<k;i++){
            cin>>a[i];
            b[a[i]]++;
        }
        if(b[0]==1){
            for(int i=1;i<n+2;i++){
                if(b[i]==1){
                    q++;
                }   
                else{
                    if(ans<q+w+1){
                    ans=q+w+1;
                    }
                    w=q;
                    q=0;
                }
            }
        }
        else{
            for(int i=1;i<n+2;i++){
                if(b[i]==1){
                    q++;
                }   
                else{
                    if(ans<q){
                    ans=q;
                    }
                    q=0;
                }
            }
        }
        cout<<ans<<endl;
        cin>>n;
    }
    return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 5084
final_rank: 17

code_snippet: #include<iostream>
#include<string>
#include<algorithm>
#include<vector>
#include<queue>
#include<map>
#include<math.h>
#include<iomanip>
#include<set>
#include<numeric>
#include<cstring>
#include<cstdio>
#include<functional>
#include<bitset>
#include<limits.h>
#include<cassert>
#include<iterator>
#include<complex>
#include<stack>
#include<sstream>
#include<iterator>
#include<list>

using namespace std;

#define INF LLONG_MAX/5
#define MOD 1000000007
#define pi 3.14159265358979
#define int long long
#define rep(i, n) for (int i = 0; i < n; i++)
#define sort(v) sort((v).begin(), (v).end())
#define reverse(v) reverse((v).begin(), (v).end())
#define upper(v,hoge) upper_bound(v.begin(),v.end(),hoge)
#define lower(v,hoge) lower_bound(v.begin(),v.end(),hoge)
#define enld endl

void vout(vector<int>A) {
	rep(i, A.size()) {
		cout << A[i];
		if (i == A.size() - 1) {
			cout << endl;
		}
		else {
			cout << ' ';
		}
	}
}

void vvout(vector<vector<int>>A) {
	rep(i, A.size()) {
		rep(j, A[i].size()) {
			cout << A[i][j];
			if (j == A[i].size() - 1) {
				cout << endl;
			}
			else {
				cout << ' ';
			}
		}
	}
}

signed main() {
	int N, K;
	while (1) {
		bool zero = false;
		cin >> N >> K;
		if (K == 1) {
			cout << 1 << enld;
		}
		else {
			if (N == 0 && K == 0) {
				return 0;
			}
			else {
				vector<int>V(K);
				rep(i, K) {
					cin >> V[i];
				}
				sort(V);
				if (V[0] == 0) {
					zero = true;
					V.erase(V.begin());
				}
				if (zero) {
					int cnt = 0;
					int ans = 0;
					vector<bool>F;
					vector<int>S;
					rep(i, V.size()) {
						if (i == 0) {
							cnt = 1;
						}
						else {
							if (V[i] - V[i - 1] == 1) {
								cnt++;
							}
							else if (V[i] - V[i - 1] == 2) {
								S.push_back(cnt);
								cnt = 1;
								F.push_back(true);
							}
							else {
								S.push_back(cnt);
								cnt = 1;
								F.push_back(false);
							}
						}
					}
					if (cnt != 1) {
						S.push_back(cnt);
					}
					rep(i, S.size()) {
						ans = max(ans, S[i]);
					}
					rep(i, S.size() - 1) {
						if (F[i]) {
							ans = max(ans, S[i] + S[i + 1] + 1);
						}
					}
					cout << ans << endl;

				}
				else {
					int cnt = 0;
					int ans = 0;
					rep(i, V.size()) {
						if (i == 0) {
							cnt = 1;
						}
						else {
							if (V[i] - V[i - 1] == 1) {
								cnt++;
							}
							else {
								ans = max(ans, cnt);
								cnt = 1;
							}
						}
					}
					cout << ans << endl;
				}
			}
		}
	}
}
accuracy: 1
time_ms: 10
memory_kb: 3796
final_rank: 12

code_snippet: #include <bits/stdc++.h>

using namespace std;

int N, K;
int DpLen[2][100002];
bool Used[100001];

int solve() {
	int maxLen;

	for (int i = 1; i <= N; ++i) {
		if (!Used[i]) {
			continue;
		}
		DpLen[0][i] = DpLen[0][i - 1] + 1;
	}
	for (int i = N; i >= 1; --i) {
		if (!Used[i]) {
			continue;
		}
		DpLen[1][i] = DpLen[1][i + 1] + 1;
	}

	maxLen = 1;


	for (int i = 1; i <= N; ++i) {
		maxLen = max(maxLen, max(DpLen[0][i], DpLen[1][i]));
		if (!Used[0] || Used[i]) {
			continue;
		}
		maxLen = max(maxLen, DpLen[0][i - 1] + 1 + DpLen[1][i + 1]);
	}

	return maxLen;
}

int main() {
	int a;

	while (cin >> N >> K, N) {
		memset(DpLen, 0, sizeof(DpLen));
		memset(Used, false, sizeof(Used));
		for (int i = 0; i < K; ++i) {
			cin >> a;
			Used[a] = true;
		}
		cout << solve() << endl;
	}

	return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 3964
final_rank: 13

code_snippet: #include <bits/stdc++.h>

bool compare(const std::pair<int, int> &a, const std::pair<int, int> &b) {
    return a.second - a.first > b.second - b.first;
}

int main() {
    int N, k, i;
    for (;;) {
        std::scanf("%d %d", &N, &k);
        if (N == 0 && k == 0) break;
        int arr[k];
        int cnt0 = 0;
        std::vector< std::pair<int, int> > v;
        for (i = 0; i < k; i++) {
            std::scanf("%d", &arr[i]);
        }
        std::sort(arr, arr+k);
        for (i = 0; i < k; i++) {
            if (arr[i] != 0) break;
            cnt0++;
        }
        int idx = i;
        for (i = idx+1; i < k; i++) {
            if (arr[i] - arr[i-1] > 1) {
                v.push_back({idx, i-1});
                idx = i;
            }
        }
        v.push_back({idx, i-1});
        if (cnt0 == 0) {
            std::sort(v.begin(), v.end(), compare);
            std::cout << v[0].second - v[0].first + 1 << std::endl;
        } else {
            int global_max = 0, current_max = 0;
            int diff;
            for (int j = 0; j < v.size()-1; j++) {
                diff = cnt0;
                current_max = v[j].second - v[j].first+1;
                if (arr[v[j+1].first] - arr[v[j].second]-1 > diff) {
                    current_max += diff;
                    global_max = std::max(global_max, current_max);
                }
                else {
                    for (int k = j+1; k < v.size(); k++) {
                        if (arr[v[k].first]-arr[v[k-1].second]-1 > diff) {
                            current_max += diff;
                            break;
                        }
                        current_max += arr[v[k].second]-arr[v[k-1].second];
                        diff -= arr[v[k].first] - arr[v[k-1].second]-1;
                    }
                    global_max = std::max(global_max, current_max);
                }
            }
            std::cout << global_max << std::endl;
        }
    }

    return  0;
}
accuracy: 1
time_ms: 10
memory_kb: 3560
final_rank: 8

code_snippet: #include <bits/stdc++.h>

#define ALL(a) (a).begin(), (a).end()

using namespace std;

signed main(){
	while(true) {
		int n; cin >> n;
		if(n == 0) break;
		int k; cin >> k;
		vector<int> arr(k);
		for(auto &&e : arr) cin >> e;
		sort(ALL(arr));
		int ans = 0;
		if(arr[0] == 0) {
			int acc = 1;
			int prev = 0;
			for(size_t i = 2; i < k; i++) {
				switch(arr[i] - arr[i-1]) {
					case 1:
						acc += 1;
						break;
					case 2:
						ans = max(ans, acc + prev + 1);
						prev = acc;
						acc = 1;
						break;
					default:
						ans = max(ans, acc + prev + 1);
						acc = 1;
						prev = 0;
						break;
				}
			}
			ans = max(ans, acc + prev + 1);
		} else {
			int acc = 1;
			for(size_t i = 1; i < k; i++) {
				if(arr[i] - arr[i-1] == 1) {
					acc += 1;
				} else {
					ans = max(ans, acc);
					acc = 1;
				}
			}
			ans = max(ans, acc);
		}
		cout << ans << "\n";
	}
	return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 3020
final_rank: 1