akhauriyash/Code-Regression huggingface dataset:
#include<bits/stdc++.h>
#define ll long long 
#define rep(i,n) for(int i=0;i<n;i++)
#define P pair<int,int>
using namespace std;

//２で割れる回数を数える関数
int cnt(int x){
    int ans = 0;
    while(x%2==0){
        ans++;
        x/=2;
    }
    return ans;
}

//最小公倍数、最大公約数(ユークリッドの互除法)ABC148
int gcd(int a,int b){return b?gcd(b,a%b):a;}
int lcm(int a,int b){return a/gcd(a,b)*b;}

int main(){
    //入力
    int n,m;
    cin >> n >> m;
    vector<int> a(n);
    rep(i,n) cin >> a[i];

    //例外消去
    rep(i,n){
        if(a[i]%2 != 0){
            cout << "0" << endl;
            return 0;
        }
        a[i] /= 2; //a -> a'
    }
    int t = cnt(a[0]);
    rep(i,n){    
        if(t!=cnt(a[i])){
            cout << "0" << endl;
            return 0;
        }
        a[i] >>= t;  //a' -> a"
    }
    m >>= t;

    ll lc = 1;
    rep(i,n){
        lc = lcm(lc,a[i]);
        if(lc>m){
            cout << "0" << endl;
            return 0;
        }
    }

    m /= lc;  //M以下のLCMの倍数の数
    int ans = (m+1)/2;  //そのうち奇数の数（切り上げ）
    cout << ans << endl;

    return 0;
}

Measured memory_bytes: 640
# include <bits/stdc++.h>
using namespace std;

int main(){
  ios::sync_with_stdio(false);
  int n; cin >> n;
  double x;
  
  if(n % 2==0){
    x = 0.5;
  }
  else{
    x = (n / 2+1) / double(n) ;
    //x = 1.0;
  }
  
  cout << x;
    
}


Measured memory_bytes: 256
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        final Scanner scanner = new Scanner(System.in);
        final int i = scanner.nextInt();
        final int j = scanner.nextInt();
        final int k = scanner.nextInt();

        System.out.println(Math.min(i * j, k));
    }
}


Measured memory_bytes: 21716
#include <cstdio>

const int mod = 1e9 + 7, maxn = 1e6 + 10;

int n, ans, f[maxn], g[maxn], sum[maxn];

int main() {
	scanf("%d", &n);
	f[n] = n - 1;
	g[n] = n;
	sum[n] = n;
	for (int i = n - 1; i; --i) {
		f[i] = (1ll * (n - 1) * (n - 1) + sum[i + 3] + (i == n - 1 ? n - 1 : i + 1)) % mod;
		g[i] = (f[i] + g[i + 1]) % mod;
		sum[i] = (sum[i + 1] + g[i]) % mod;
	}
	printf("%d\n", g[1]);
	return 0;
}

Measured memory_bytes: 11904
#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
#include <numeric>
    
using namespace std;    
typedef long long ll;
#define all(x) (x).begin(),(x).end()
#define rall(x) (x).rbegin(),(x).rend()
const bool DEBAG = false;

int H;
int W;
char grid[2000][2000];

int main(){

    cin >> H >> W;
    vector < vector<int> > objPosWidth(H);
    vector < vector<int> > objPosHeight(W);

    for(int i = 0; i < H; i++) objPosWidth[i].push_back(-1);
    for(int i = 0; i < W; i++) objPosHeight[i].push_back(-1);

    for(int x = 0; x < H; x++){
        for(int y = 0; y < W; y++){
            cin >> grid[x][y];
            if(grid[x][y] == '#'){
                objPosWidth[x].push_back(y);
                objPosHeight[y].push_back(x);
            }
        }
    }

    for(int i = 0; i < H; i++) objPosWidth[i].push_back(W);
    for(int i = 0; i < W; i++) objPosHeight[i].push_back(H);


    int width[H][W] ;  // 横の数
    int height[H][W] ;
    for(int x = 0; x < H; x++){
        for(int y = 0; y < W; y++){
            width[x][y] = 0;
            height[x][y] = 0;
        }
    }

    for(int x = 0; x < H; x++){
        int obj_ind = 0;
        for(int y = 0; y < W; y++){
            if(y == objPosWidth[x][obj_ind + 1]){
                width[x][y] = 0;
                obj_ind ++;
            }
            else{
                width[x][y] = objPosWidth[x][obj_ind + 1] - objPosWidth[x][obj_ind ] - 1;
            }
        }
    }

    for(int y = 0; y < W; y++){
        int obj_ind = 0;
        for(int x = 0; x < H; x++){
            if(x == objPosHeight[y][obj_ind + 1]){
                height[x][y] = 0;
                obj_ind ++;
            }
            else{
                height[x][y] = objPosHeight[y][obj_ind + 1] - objPosHeight[y][obj_ind ] - 1;
            }
        }
    }

    if(DEBAG){
        for(int x = 0; x < H; x++){
            for(int y = 0; y < objPosWidth[x].size(); y++){
                cout << objPosWidth[x][y];
            }
            cout << endl;
        }


        for(int x = 0; x < H; x++){
            for(int y = 0; y < W; y++){
                cout << width[x][y];
            }
            cout << endl;
        }
    }


    int ans = 0;
    for(int x = 0; x < H; x++){
        for(int y = 0; y < W; y++){
            ans = max(ans, width[x][y] + height[x][y]);
        }
    }
    cout << ans - 1<< endl;


}

Measured memory_bytes: 71680
import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int total = sc.nextInt();
        int[] buttons = new int[total + 1];
        for (int i = 1; i <= total; i++) {
            buttons[i] = sc.nextInt();
        }
        int num = 1;
        int i = 0;
        do {
                num = buttons[num];
                i++;
                if (num == 2){
                        break;
                }
        } while (i <= total);
        if (num == 2) {
                System.out.println(i);
        }
        else {
                System.out.println("-1");
        }
    }
}

Measured memory_bytes: 50588
#include<iostream>
#include<algorithm>
#include<set>
using lint=long long;
using namespace std;

lint s;
lint a[1000010];
set<lint> ss;

lint f(lint n)
{
	if(n%2==0)
	{
		return n/2;
	}
	else return 3*n+1;
}

lint aa(lint i)
{
	if(a[i]!=0)return a[i];
	if(i==1)
	{
		a[1]=s;
	}else{
		a[i]=f(aa(i-1));
	}
	return a[i];
}

int main()
{
	cin >> s;
	
	for(lint m=1;;m++)
	{
		lint an=aa(m);
		auto it=ss.find(an);
		if(it!=ss.end())
		{
			cout << m << endl;
			return 0;
		}
		ss.insert(an);
	}
	return 0;
}



Measured memory_bytes: 256
#include <iostream>
using namespace std;
int main(){
    int h,w;
    while(1){
    cin >> h >> w;
    for(int i = 0; i < h; i++){
        for(int j = 0;j < w; j++){
        if((i==0)||(i==h-1)||(j==0)||(j==w-1)){ 
        cout << '#';
        }else cout << '.';
        }
        cout << "\n";
}
      if(h==0 && w==0) break;
        cout << "\n";
    }
    return 0;
}


Measured memory_bytes: 2992
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define ll long long
#define P pair<int,int>
#define fi first
#define se second
#define rep(i,n) for(int i=0;i<n;i++)
#define all(v) v.begin(),v.end()
#define pb push_back
template<class T>void chmax(T &a,T b){if(a<b)a=b;}
template<class T>void chmin(T &a,T b){if(a>b)a=b;}
constexpr int INF=1000000000000000000;
constexpr int mod=1000000007;
int dx[]={0,1,0,-1},dy[]={1,0,-1,0};
int kaijo[2000010];
int gcd(int a,int b){
    if(b==0)return a;
    return gcd(b,a%b);
}
int lcm(int a,int b){
    return a/gcd(a,b)*b;
}
bool prime(int a){
    if(a==1)return false;
    for(int i=2;i*i<=a;i++){
        if(a%i==0)return false;
    }
    return true;
}
void init_fact(){
    kaijo[0]=1;
    for(int i=1;i<=2000000;i++){
        kaijo[i]=kaijo[i-1]*i;
        kaijo[i]%=mod;
    }
}
int modpow(int a,int b){
    if(b==0)return 1;
    if(b%2)return modpow(a,b-1)*a%mod;
    int memo=modpow(a,b/2);
    return memo*memo%mod;
}
int comb(int a,int b){
    if(!kaijo[0])init_fact();
    return kaijo[a]*modpow(kaijo[a-b],mod-2)%mod*modpow(kaijo[b],mod-2)%mod;
}
int inv(int x){
    x=modpow(x,mod-2);
    return x;
}
bool kosa(double ax,double ay,double bx,double by,double cx,double cy,double dx,double dy){
    double ta=(cx-dx)*(ay-cy)+(cy-dy)*(cx-ax);
    double tb=(cx-dx)*(by-cy)+(cy-dy)*(cx-bx);
    double tc=(ax-bx)*(cy-ay)+(ay-by)*(ax-cx);
    double td=(ax-bx)*(dy-ay)+(ay-by)*(ax-dx);
    return tc*td<0&&ta*tb<0;
}
int n,a[18],b[18];
int dp[(1<<18)][55];
int ans=INF;
signed main(){
    cin>>n;
    rep(i,n)cin>>a[i];
    rep(i,n)cin>>b[i];
    rep(i,(1<<n)){
        rep(j,55){
            dp[i][j]=INF;
        }
    }
    dp[0][0]=0;
    rep(i,(1<<n)){
        int pos=__builtin_popcount(i);
        vector<int>sum(n+1,0);
        for(int j=n-1;j>=0;j--){
            sum[j]=sum[j+1]+((i>>j)&1);
        }
        rep(j,n){
            if(!((i>>j)&1)){
                //dp[i] -> dp[i+(1<<j)]
                int last=(abs(j-pos)%2==0?a[j]:b[j]);
                rep(k,last+1){
                    chmin(dp[i+(1<<j)][last],dp[i][k]+abs(j+sum[j]-pos));
                }
            }
        }
    }
    rep(i,51)chmin(ans,dp[(1<<n)-1][i]);
    cout<<(ans==INF?-1:ans)<<endl;
    return 0;
}


Measured memory_bytes: 115072
N = int(input())
V = []
for _ in range(N-1):
    u, v, w = map(int, input().split())
    V.append((u,v,w))
    
edges = V[:]

from heapq import heappush, heappop

def dijkstra(graph, start):
    A = [None] * (N)
    queue = [(0, start)]
    while queue:
        path_len, v = heappop(queue)
        if A[v] is None: # v is unvisited
            A[v] = path_len
            for w, edge_len in graph[v].items():
                if A[w] is None:
                    heappush(queue, (path_len + edge_len, w))

    # to give same result as original, assign zero distance to unreachable vertices             
    return [0 if x is None else x for x in A]



# 無効グラフなので双方向にする
graph = {}
for a,b,c in edges:
    a = a-1
    b = b-1
    if not a in graph:
        graph[a] = {}
    if not b in graph:
        graph[b] = {}
    graph[a][b] = c
    graph[b][a] = c
    
    
shortest = dijkstra(graph, 0)

for s in shortest:
    print(s % 2)

Measured memory_bytes: 69448
from itertools import accumulate    # 累積和: list(accumulate(num_list))

def main():
    n, k = map(int, input().split())
    num_list = list(map(int, input().split()))

    compute_list = []

    for num in num_list:
        compute_list.append((1+num)/2)

    sum_list = list(accumulate(compute_list))

    if k == n:
        print(sum_list[k-1])
        exit()

    ans = 0

    for i in range(n-k):
        ans = max(ans, sum_list[i+k] - sum_list[i])

    print(ans)

if __name__ == '__main__':
    main()

Measured memory_bytes: 77448
#include<stdio.h>
int main(void)
{
	int n,m,d,cnt,i,j,k,flg,flg2;
	char s[101][101];
	cnt=0;
	scanf("%d %d %d",&n,&m,&d);
	 for(i=0;i<n;i++){
			 scanf("%s",s[i]);
		 }
	 for(i=0;i<n;i++){
		 for(j=0;j<m;j++){
			 flg=0;
			 flg2=0;
			 for(k=1;k<d;k++){
				 if(!(s[i][j]=='.'&&s[i+k][j]=='.')){
					 flg=1;
				 }
				 if(!(s[i][j]=='.'&&s[i][j+k]=='.')){
					 flg2=1;
				 }
			 }
			 if(flg==0){
				 cnt++;
			 }
			 if(flg2==0){
				 cnt++;
			 }
		 }
	 }
	 printf("%d\n",cnt);
	 return 0;
}


Measured memory_bytes: 2144
#include <bits/stdc++.h>

using namespace std;

using int64 = long long;

struct UnionFind
{
  vector< int > data;

  UnionFind(int sz) : data(sz, -1) {}

  bool unite(int x, int y)
  {
    x = find(x), y = find(y);
    if(x == y) return (false);
    if(data[x] > data[y]) swap(x, y);
    data[x] += data[y];
    data[y] = x;
    return (true);
  }

  int find(int k)
  {
    if(data[k] < 0) return (k);
    return (data[k] = find(data[k]));
  }

  int size(int k)
  {
    return (-data[find(k)]);
  }
};

struct edge1
{
  int u, v, cost, idx;

  bool operator<(const edge1 &e) const
  {
    return (cost < e.cost);
  }
};

struct edge2
{
  int to, cost, idx;
};

int N, M, A[200000], B[200000], C[200000];
vector< edge1 > edges;
bool killed[200000];
vector< edge2 > tree[100000], g[100000];
int64 all, ans[200000];
set< pair< int, int > > component[100000];

void rec(int idx, int par = -1)
{
  for(auto &e : g[idx]) {
    component[idx].emplace(e.cost, e.idx);
  }

  for(auto &e : tree[idx]) {
    if(e.to == par) continue;
    rec(e.to, idx);

    auto &u = component[idx], &v = component[e.to];

    if(!v.empty()) {
      ans[e.idx] = all;
      ans[e.idx] -= e.cost;
      ans[e.idx] += v.begin()->first;
    }
    if(u.size() < v.size()) swap(u, v);

    while(!v.empty()) {
      auto p = v.begin();
      if(u.count(*p)) u.erase(*p);
      else u.emplace(*p);
      v.erase(p);
    }
  }
}

int main()
{
  scanf("%d %d", &N, &M);
  for(int i = 0; i < M; i++) {
    scanf("%d %d %d", &A[i], &B[i], &C[i]);
    --A[i], --B[i];
    edges.push_back((edge1) {A[i], B[i], C[i], i});
  }

  UnionFind uf(N);
  sort(begin(edges), end(edges));
  for(auto &e : edges) {
    if(uf.unite(e.v, e.u)) {
      killed[e.idx] = true;
      all += e.cost;
    }
  }

  if(uf.size(0) != N) {
    for(int i = 0; i < M; i++) puts("-1");
    return (0);
  }

  for(int i = 0; i < M; i++) {
    if(killed[i]) {
      tree[A[i]].emplace_back((edge2) {B[i], C[i], i});
      tree[B[i]].emplace_back((edge2) {A[i], C[i], i});
      ans[i] = -1;
    } else {
      g[A[i]].emplace_back((edge2) {B[i], C[i], i});
      g[B[i]].emplace_back((edge2) {A[i], C[i], i});
      ans[i] = all;
    }
  }

  rec(0);

  for(int i = 0; i < M; i++) {
    printf("%lld\n", ans[i]);
  }
}

Measured memory_bytes: 46052
#include <bits/stdc++.h>

using namespace std;

struct Point {
    int x, y;
    Point(int _x, int _y) : x(_x), y(_y) {}
};

inline void Next(Point &cur, int &dir, const int w) {
    cur.x += dir;
    if (cur.x < 0 || w <= cur.x) {
        dir *= -1;
        cur.x += dir;
        ++cur.y;
    }
}

void Solve() {
    int h, w, cnt = 0;

    cin >> h >> w;
    vector<vector<bool>> a(h, vector<bool>(w, false));
    for (int i = 0; i < h; ++i) {
        for (int j = 0, x; j < w; ++j) {
            cin >> x;
            cnt += a[i][j] = (x % 2 == 1);
        }
    }

    Point cur(0, 0);
    int dir = 1;
    vector<Point> move;

    for (int i = 1; 2 * i <= cnt; ++i) {
        while (!a[cur.y][cur.x]) Next(cur, dir, w);
        Point nxt = cur;
        do {
            Next(nxt, dir, w);
            move.emplace_back(cur);
            move.emplace_back(nxt);
            cur = nxt;
        } while (!a[cur.y][cur.x]);
        Next(cur, dir, w);
    }

    const int size = move.size();
    cout << size / 2 << '\n';
    for (int i = 0; i < size; i += 2) {
        cout << move[i].y + 1 << ' ' << move[i].x + 1 << ' '
             << move[i + 1].y + 1 << ' ' << move[i + 1].x + 1 << '\n';
    }
}

int main() {
    cin.tie(0); ios::sync_with_stdio(false);

    Solve();

    return 0;
}


Measured memory_bytes: 4212
#include <iostream>
#include <cmath>
using namespace std;

const char NL = '\n';
const char SP = ' ';
#define loop(i, a, b) for (int i = a; i < b; i++)

int main () {
  ios_base::sync_with_stdio(false);
  double n, x, t;
  cin >> n >> x >> t;
  double num = ceil (n / x);
  num = ceil (num);
  cout << int(num * t) << NL;
  return 0;
}

Measured memory_bytes: 3688
#include <bits/stdc++.h>
using namespace std;

int main(){
  long long A, B;
  cin >> A >> B;
  
  if (A - B >= 0){
    if ((A - B) % 2 == 0){
      cout << (A - B) / 2 + B << endl;
    }
    else{
      cout << "IMPOSSIBLE" << endl;
    }
  }
  else{
    if ((B - A) % 2 == 0){
      cout << (B - A) / 2 + A<< endl;
    }
    else{
      cout << "IMPOSSIBLE" << endl;
    }
  }
}

Measured memory_bytes: 3596
N = int(input())
A = list(map(int, input().split()))
B = list(map(int, input().split()))

if sum(B) > sum(A):
    print(-1)

ab = [0]*N
for i in range(N):
    ab[i] = A[i]-B[i]

ab.sort()

ans = 0
minus_sum = 0
for i in range(N):
    if ab[i] >= 0:
        break
    minus_sum += ab[i]
    ans += 1

for i in reversed(range(N)):
    if minus_sum >= 0:
        print(ans)
        exit(0)

    minus_sum += ab[i]
    ans += 1



Measured memory_bytes: 19068
#include <iostream>

using namespace std;

int n;
int a[100005][3];
int dp[100005][3];

int main(void)
{
	cin >> n;
	for(int i = 1;  i<= n; i++){
		for(int j = 0 ; j< 3;j++) cin >> a[i][j];
	}
	
	for(int i = 1; i <= n; i++){
		for(int j = 0; j < 3; j++) dp[i][j] = -1e9;
	}
	for(int i = 0; i < 3; i++) dp[1][i] = a[1][i];
	
	for(int i = 1; i < n; i++){
		for(int j = 0; j < 3; j++){
			for(int k = 0; k < 3; k++){
				if(j == k) continue;
				dp[i+1][k] = max(dp[i+1][k], dp[i][j] + a[i+1][k]);
			}
		}
	}
	
	int ans = 0;
	for(int i = 0; i < 3; i++) ans = max(ans, dp[n][i]);
	cout << ans << endl;
	
	return 0;
}

Measured memory_bytes: 2560
#include<bits/stdc++.h>
using ll = long long;
const ll INF_LL = 1000000000000;
#define fir first
#define sec second
using namespace std;

int main()
{
    std::string s;
    std::cin >> s;
    int n = s.size(), t = 1;
    int w = 0, l = 0;
    for(int i = 1; i < n; i++){
        if(s[i] == 'p' && t > 0)t--;
        else if(s[i] == 'p' && t == 0){l++;t++;}
        else if(s[i] == 'g' && t > 0){t--;w++;}
        else if(s[i] == 'g' && t == 0)t++;
        //std::cout << w << " " << l << std::endl;
    }
    std::cout << w - l << std::endl;
}


Measured memory_bytes: 512
n = int(input())
ai = list(map(int, input().split()))

ai.sort()

i1 = -1
i2 = -1
b = 0
for i in range(n - 1, b - 1, -1):
    if ai[i - b] == ai[i - 1 - b]:
        b += 1
        if i1 == -1:
            i1 = ai[i - b]
        else:
            i2 = ai[i - b]
            print(i1 * i2)
            break

if i2 == -1:
    print(0)


Measured memory_bytes: 14252
#include <iostream>
#include <fstream>
#include <sstream>
#include <iomanip>
#include <vector>
#include <string>
#include <algorithm>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <unordered_set>
#include <unordered_map>
#include <bitset>
#include <limits>
#include <random>
#include <complex>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstdint>
using namespace std;

#define REP(i,n) for(int (i)=0;(i)<(int)(n);(i)++)
#define RREP(i,n) for(int (i)=(int)(n)-1;i>=0;i--)
#define REMOVE(Itr,n) (Itr).erase(remove((Itr).begin(),(Itr).end(),n),(Itr).end())
typedef long long ll;


int main () {

    int A,B;
    cin >> A >> B;

    cout << A * B - (A + B) + 1 << endl;

    return 0;
}


Measured memory_bytes: 256
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#define MAXN 400010

int n, a[MAXN];
double ans = 0;

int main() {
	scanf("%d", &n);
	for (int i = 0; i < 2 * n; i++) scanf("%d", &a[i]);
	std::sort(a, a + 2 * n);
	for (int i = 0; i < 2 * n; i++) ans += 1. * i * a[i] / (2 * n - 1);
	printf("%.10lf\n", ans);
	return 0;
}

Measured memory_bytes: 896
import bisect

n = int(input())
l = list(map(int,input().split()))
count = 0

l_s = sorted(l)

for i in range(n-2):
    for j in range(i+1,n-1):
        a = l_s[i] + l_s[j]        
        b = bisect.bisect_left(l_s, a)
        count += max(0,b - j-1)
        
print(count)

Measured memory_bytes: 3316
import heapq
from sys import stdin
input = stdin.readline

#入力
# s = input()[0:-1]
# n = int(input())
# a = list(map(int,input().split()))
# a = [int(input()) for i in range()]

a,b = map(int, input().split())
# ab=[]
# for i in range():
#     a,b = map(int, input().split())
#     ab.append([a,b])


     
def main():
    print(a*b)
    
if __name__ == '__main__':
    main()

Measured memory_bytes: 9196
#include <bits/stdc++.h>
using namespace std;

typedef pair <int, int> ii;

const int Maxn = 100005;

int n;
int a[Maxn], b[Maxn];
vector <ii> A, B;
int p[Maxn];

int main()
{
    scanf("%d", &n);
    for (int i = 0; i < n; i++) {
        scanf("%d", &a[i]);
        A.push_back(ii(a[i], i));
    }
    for (int i = 0; i < n; i++) {
        scanf("%d", &b[i]);
        B.push_back(ii(b[i], i));
    }
    sort(A.begin(), A.end());
    sort(B.begin(), B.end());
    for (int i = 0; i < n; i++)
        if (A[i].first > B[i].first) { printf("No\n"); return 0; }
    for (int i = 0; i + 1 < n; i++)
        if (A[i + 1].first <= B[i].first) { printf("Yes\n"); return 0; }
    for (int i = 0; i < n; i++) {
        int ind = lower_bound(A.begin(), A.end(), ii(a[i], i)) - A.begin();
        p[i] = B[ind].second;
    }
    int tk = 0, v = 0;
    while (p[v] != -1) {
        int u = p[v]; p[v] = -1;
        tk++; v = u;
    }
    printf("%s\n", tk < n? "Yes": "No");
    return 0;
}


Measured memory_bytes: 3188
using System;
using System.Linq;
 
namespace AtCoder
{
    class HappyBirthday
    {
        public static void Main(string[] args)
        {
            var dn = Console.ReadLine().Split(' ').Select(s => int.Parse(s));
            int d = dn.First();
	        int n = dn.Last();
          
            int m = (int)Math.Pow(100, d);
            Console.WriteLine(n == 100 ? m * 101 : m * n);
        }
    }
}

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

int main(){
    int A, B;
    cin >> A >> B;
    if(B % A == 0){
        cout << A + B << endl;
    }
    else{
        cout << B - A << endl;
    }
    return 0;
}

Measured memory_bytes: 256
#include "bits/stdc++.h"
#define Rep(i,n) for(int i=0;i<n;i++)
#define For(i,n1,n2) for(int i=n1;i<n2;i++)
#define REP(i,n) for(ll i=0;i<n;i++)
#define FOR(i,n1,n2) for(ll i=n1;i<n2;i++)
#define put(a) cout<<a<<endl;
#define all(a)  (a).begin(),(a).end()
#define SORT(c) sort((c).begin(),(c).end())
#define TDARRAY(int,a,n,m) vector<vector<int>> a(n,vector<int>(m,0));
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
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;}
 
int n,m;
 
int main(){
    cin >> n >> m;
    vector<int> a(m);
    vector<int> b(m);
    vector<ll> c(m);
    REP(i,m){
        cin >> a[i] >> b[i] >> c[i];
        --a[i];
        --b[i];
        c[i]*=-1;
    }
    //Bellman-Ford法
    vector<ll> dist(n,1e15);
    int src = 0;
    dist[src]=0;
    REP(i,n){
        bool flag = false;
        REP(j,m){
            if(chmin(dist[b[j]],dist[a[j]]+c[j])){
                if (b[j] == n - 1) flag = true;
            }
        }
        if(i==n-1&&flag){
            put("inf");
            return 0;
        }
    }
    put(-dist.back());
    return 0;
}

Measured memory_bytes: 256
let s = read_line ()
let a_s = Array.make (String.length s + 1) 0
let _ = String.(iteri (fun i c -> if c = '<' then a_s.(i + 1) <- a_s.(i) + 1) s; for i = length s - 1 downto 0 do if s.[i] = '>' then a_s.(i) <- max a_s.(i) @@ a_s.(i + 1) + 1 done); Array.fold_left (+) 0 a_s |> Printf.printf "%d\n"

Measured memory_bytes: 8704
#include <bits/stdc++.h>

using namespace std;

typedef pair<int, int> II;
typedef vector<II> VII;
typedef vector<int> VI;
typedef vector<VI> VVI;

typedef long long int LL;

#define PB push_back
#define MP make_pair
#define F first
#define S second
#define SZ(a) (int)(a.size())
#define ALL(a) a.begin(), a.end()
#define SET(a, b) memset(a, b, sizeof(a))
#define si(n) scanf("%d", &n)
#define dout(n) printf("%d\n", n)
#define sll(n) scanf("%lld", &n)
#define lldout(n) printf("%lld\n", n)

#define TRACE

#ifdef TRACE
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1>
void __f(const char *name, Arg1 &&arg1) {
  cerr << name << " : " << arg1 << std::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...);
}
#else
#define trace(...)
#endif

#define fast_io                     \
  ios_base::sync_with_stdio(false); \
  cin.tie(NULL)

#define endl '\n'
const LL MOD = 1000000007;
int main() {
  fast_io;
  int n;
  cin >> n;
  string a, b;
  cin >> a >> b;
  int smlen = 0;
  int eqlen = 0;
  LL ans = 3;
  int fl = a[0] == b[0] ? 0 : 1;
  LL sslen, seqlen;
  sslen = seqlen = 0;
  for (int i = 0; i < n; i++) {
    int upd = 0;
    if (fl && i + 1 < n && a[i] == a[i + 1] && b[i] == b[i + 1])
      smlen++, i++;
    else if (!fl && a[i] == b[i])
      eqlen++;
    else if (smlen > 0) {
      upd = 1;
      smlen--;
      ans = (ans + ans) % MOD;
      while (smlen--) ans = (ans + ans + ans) % MOD;
      smlen = 0;
    } else if (eqlen > 0) {
      seqlen += eqlen;
      upd = 1;
      trace(i, eqlen);
      eqlen--;
      while (eqlen--) ans = (ans + ans) % MOD;
      eqlen = 0;
    }
    if (upd) {
      if (a[i] == b[i])
        fl = 0;
      else
        fl = 1;
      i--;
    }
  }
  sslen += smlen;
  seqlen += eqlen;
  if (smlen > 0) {
    smlen--;
    ans = (ans + ans) % MOD;
    while (smlen--) ans = (ans + ans + ans) % MOD;
  }
  if (eqlen > 0) {
    eqlen--;
    trace(eqlen);
    while (eqlen--) ans = (ans + ans) % MOD;
  }
  trace(sslen, seqlen);
  cout << ans << endl;
  return 0;
}


Measured memory_bytes: 256
#include <bits/stdc++.h>
#include <stdio.h>

#define REP(i, n) for (int i=0; i<n; i++)
#define print(x) cout << x << endl

typedef long long ll;

using namespace std;

void solve() {
    ll N, K;
    cin >> N >> K;
    vector<ll> A(N), F(N);

    REP(i, N) cin >> A[i];
    REP(i, N) cin >> F[i];

    sort(A.begin(), A.end(), greater<ll>());
    sort(F.begin(), F.end());

    /*
    REP(i, N) {
        cout << A[i];
        if (i != N-1) cout << " ";
        else cout << endl;
    }
    REP(i, N) {
        cout << F[i];
        if (i != N-1) cout << " ";
        else cout << endl;
    }
    */

    ll l = -1;
    ll r = 1001001001001001001;
    while (r - l > 1) {
        ll m = (l + r) / 2;
        ll count = 0;
        for (int i = 0; i < N; i++) {
            double diff = double(m) - F[i] * A[i];
            ll tmp = 0;
            if (diff < 0) {
                tmp = A[i] - floor(double(m) / F[i]);
                count += tmp;
            }
            //cout << A[i] << " " << F[i] << " " << tmp << endl;
        }
        //print(m << " " << count);
        if (count > K) {
            l = m;
        } else {
            r = m;
        }
    }
    print(r);
}

signed main() {
    solve();
}


Measured memory_bytes: 3456
#include <bits/stdc++.h>
using namespace std;

long long comb(int x) {
	return (long long) x * (x - 1) / 2;
}

int main() {
	int n;
	cin >> n;
	vector<string> s(n);
	for (int i = 0; i < n; i++) {
		cin >> s.at(i);
		sort(s.at(i).begin(), s.at(i).end());
	}
	sort(s.begin(), s.end());
	long long sum = 0;
	int count = 0;
	for (int i = 1; i < n; i++) {
		if (s.at(i) == s.at(i - 1))
			count++;
		else {
			sum += comb(count+1);
			count = 0;
		}
	}
	sum += comb(count+1);
	cout << sum<< endl;
}


Measured memory_bytes: 7296
import java.io.*;
import java.util.Arrays;
import java.util.StringTokenizer;

public class Main {

    static class Task {

        private long pow5(int a) {
            long ans = 1;
            for (int i = 0; i < 5; i++) {
                ans *= a;
            }

            return ans;
        }

        public void solve(int testNumber, InputReader in, PrintWriter out) {
            int x = in.nextInt();
            // 求A^5 - B^5 = X
            // A > B, 两种情况
            // 1. A B都是正数
            // 2. A是正数，B是负数
            for (int b = -200; b <= 200; b++) {
                for (int a = -200; a <= 200; a++) {
                    long powA = pow5(a);
                    long powB = pow5(b);
                    if (powA - powB == x) {
                        out.println(a + " " + b);
                        return;
                    }
                }
            }
        }
    }

    private static void sort(double[] arr) {
        Double[] objArr = Arrays.stream(arr).boxed().toArray(Double[]::new);
        Arrays.sort(objArr);
        for (int i = 0; i < arr.length; i++) {
            arr[i] = objArr[i];
        }
    }

    private static void sort(int[] arr) {
        Integer[] objArr = Arrays.stream(arr).boxed().toArray(Integer[]::new);
        Arrays.sort(objArr);
        for (int i = 0; i < arr.length; i++) {
            arr[i] = objArr[i];
        }
    }

    private static void sort(long[] arr) {
        Long[] objArr = Arrays.stream(arr).boxed().toArray(Long[]::new);
        Arrays.sort(objArr);
        for (int i = 0; i < arr.length; i++) {
            arr[i] = objArr[i];
        }
    }

    private static void solve() {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        InputReader in = new InputReader(inputStream);
        PrintWriter out = new PrintWriter(outputStream);
        Task task = new Task();
        task.solve(1, in, out);
        out.close();
    }

    public static void main(String[] args) {
        new Thread(null, () -> solve(), "1", 1 << 26).start();
    }

    static class InputReader {

        public BufferedReader reader;
        public StringTokenizer tokenizer;

        public InputReader(InputStream stream) {
            reader = new BufferedReader(new InputStreamReader(stream), 32768);
            tokenizer = null;
        }

        public String next() {
            while (tokenizer == null || !tokenizer.hasMoreTokens()) {
                try {
                    tokenizer = new StringTokenizer(reader.readLine());
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
            return tokenizer.nextToken();
        }

        public int nextInt() {
            return Integer.parseInt(next());
        }

        public long nextLong() {
            return Long.parseLong(next());
        }

        public double nextDouble() {
            return Double.parseDouble(next());
        }

    }

}


Measured memory_bytes: 37192
#include "bits/stdc++.h"
using namespace std;

#define show(x) cerr << #x << " = " << x << endl ;
#define input_from_file freopen("input.txt","r",stdin)
#define output_to_file freopen("output.txt" , "w",stdout)
#define fastio ios_base::sync_with_stdio(0)
#define endl '\n'

const int N = 1e5+1;
bool dp[N];
map < int,  bool > m;
int main()
{
	 #ifdef LOCAL
        input_from_file;
        output_to_file;
    #endif
	fastio;cin.tie(0);cout.tie(0);
	int n , k , x ; cin >> n >> k ;
	for(int i = 1 ; i <= n ; ++i)
	{
		cin >> x ;
		m[x] = 1;
	}
	bool flag ;
	for(int i = 1 ; i <= k ; ++i)
	{
		if(m.find(i) != m.end()) dp[i] = 1;
		else
		{
			for(auto it = m.begin() ; it != m.end() ; ++it)
			{
				x = it -> first ;
				if(x > i) break;
				if(!dp[i-x]) dp[i] = 1;
			}
		} 

	}
	cout << (dp[k] ? "First" : "Second");

	return 0;
}

Measured memory_bytes: 384
#include <bits/stdc++.h>
using   namespace  std ;
typedef long   long LL ;

LL dp[100005];
int ww[1005], vv[1005];

int main(){

	for(int i = 1 ; i <= 100005 ; i++)
		dp[i] = INT_MAX ;
	dp[0] = 0 ;

	int N, W ;
	cin >> N >> W ;

	int sumV = 0 ;
	for(int i = 1 ; i <= N ; i++){
		cin >> ww[i] >> vv[i] ;
		sumV += vv[i] ;
	}

	for(int i = 1 ; i <= N ; i++){
		for(int j = sumV ; j >= vv[i] ; j--){
			dp[j] = min(dp[j], dp[j - vv[i]] + ww[i]) ;
		}
	}

	for(int i = sumV ; i >= 0 ; i--){
		if(dp[i] <= W){
			cout << i << endl ;
			break ;
		}
	}

	return 0;
}

Measured memory_bytes: 1024
s=list(input())
import collections 
D=collections.deque(s)
counter=0
lastindex=0
q=int(input())
for i in range(q):
    a=list(input().split())
    if int(a[0])==1:
        counter+=1

    else:
        if (counter+int(a[1]))%2==0:
            D.append(a[2])
        else:
            D.appendleft(a[2])
if counter%2==0:
    D=list(D)
    print("".join(D))
else:
    D=list(D)[::-1]
    print("".join(D))

Measured memory_bytes: 10868
#include <bits/stdc++.h>
using namespace std;
using ll = int64_t;
#define rep(i, n) for (int i = 0; i < ((int)(n)); i++) // 0-indexed昇順

int main()
{
    int H, W;
    cin >> H >> W;
    rep(i, H)
    {
        string S;
        cin >> S;
        cout << S << endl;
        cout << S << endl;
    }
}


Measured memory_bytes: 256
#define _USE_MATH_DEFINES
#include <algorithm>
#include <cstdio>
#include <functional>
#include <iostream>
#include <cfloat>
#include <climits>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <time.h>
#include <vector>
#include <memory>
using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> i_i;
typedef pair<ll, int> ll_i;
typedef pair<double, int> d_i;
typedef pair<ll, ll> ll_ll;
typedef pair<double, double> d_d;
struct edge { int v, w; };

#define rep(i, N) for (int i = 0; i < N; i++)
#define pb push_back

ll MOD = 1000000007;
ll _MOD = 1000000009;
double EPS = 1e-10;

int main() {
	int N, d0, x;
	cin >> N >> d0 >> x;
	vector<ll> d(N * 2);
	d[0] = d0;
	for (int i = 1; i < N * 2; i++)
		d[i] = d[i - 1] + x;
	vector<ll> a(N);
	rep(i, N) a[i] = d[i * 2] + d[i * 2 + 1];
	vector<double> sum(N + 1);
	rep(i, N) sum[i + 1] = sum[i] + a[i];
	double ans = sum[N] * 0.5;
	for (int d = 1; d <= N - 1; d++) {
		ans += (sum[N] - sum[d]) * 0.25 / (d + 1);
		ans += (sum[N - d] - sum[0]) * 0.25 / (d + 1);
	}
	/*rep(i, N) {
		ll D = d[i * 2] + d[i * 2 + 1];
		for (int j = max(0, i - 1000); j <= min(N - 1, i + 1000); j++) {
			int k = abs(i - j);
			if (k) ans += D * 0.25 / (k + 1);
			else ans += D * 0.5;
		}
	}*/
	printf("%.15f\n", ans);
}


Measured memory_bytes: 6656
#include<bits/stdc++.h>
using namespace std;

#define IOS ios_base::sync_with_stdio(false); cin.tie(NULL);
#define ll long long
#define vi vector< int > 
#define pii pair< int, int >
#define mii map< int , int > 
#define F(start,stop) for(int i=start; i<stop; i++)
#define FRev(start,stop) for(int i=start; i>=stop; i--)
#define MOD1 1000000007
#define MOD2 998244353


int main()
{

	#ifndef ONLINE_JUDGE
		freopen("input.txt", "r", stdin);
		freopen("output.txt", "w", stdout);
	#endif

	IOS

	int n;
	cin >> n;

	int arr[n];
	int xr = 0;

	F(0,n){
		cin >> arr[i];
		xr ^= arr[i];
	}

	F(0,n){
		cout << (xr ^ arr[i]) << " ";
	}
	cout << endl;
}

Measured memory_bytes: 4244
#include <bits/stdc++.h>
#define rep(i,n)for(int i=0;i<(n);i++)
#define INF 0x3f3f3f3f3f3f3f3f
using namespace std;
typedef pair<int,int>P;
typedef long long ll;

P p[6000];
ll dp[5001][5001];

int main(){
	int n;scanf("%d",&n);
	rep(i,n){
		scanf("%d%d",&p[i].first,&p[i].second);
	}
	sort(p,p+n,[](P a,P b){return a.first+a.second<b.first+b.second;});
	memset(dp,0x3f,sizeof(dp));
	dp[0][0]=0;
	rep(i,n)rep(j,n+1){
		dp[i+1][j]=min(dp[i+1][j],dp[i][j]);
		if(dp[i][j]<=p[i].first)dp[i+1][j+1]=min(dp[i+1][j+1],dp[i][j]+p[i].second);
	}
	int ans=0;
	rep(i,n+1){
		if(dp[n][i]!=INF)ans=i;
	}
	cout<<ans<<endl;
}

Measured memory_bytes: 195712
#include <bits/stdc++.h>
#define rep(i,n) for(int i=0, i##_len=(n); i<i##_len; ++i)
using namespace std;
using ll = long long;
using d = double;

int main(){
  int n;
  ll d;
  cin >> n >> d;
  vector<ll> xx(n);
  vector<ll> yy(n);

  rep(i,n){
    scanf("%lld%lld", &xx[i], &yy[i]);
  }
  d *= d;
  int ans=0;
  rep(i,n){
    ll p=xx[i]*xx[i]+yy[i]*yy[i];
    if(p<=d)++ans;
  }
  cout << ans << endl;

  return 0;
}


Measured memory_bytes: 6424
from itertools import permutations

n, m = map(int, input().split())

edges = {tuple(sorted(map(int, input().split()))) for _ in range(m)}
ans = 0

for i in permutations(range(2,n+1), n-1):
    l = [1] + list(i)
    tmp = sum(1 for edge in zip(l, l[1:]) if tuple(sorted(edge)) in edges) == n-1
    ans += tmp
print(ans)



Measured memory_bytes: 9160
#include <bits/stdc++.h>

using namespace std;

const int N = 2e5 + 5;

int x[N];

int main() {
  int len, n;
  scanf("%d %d", &len, &n);
  for (int i = 0; i < n; ++i) {
    scanf("%d", x+i);
  }
  long long ans = 0, now = 0;
  int l = 0, r = 0;
  for (int i = 0; i < n; ++i) {
    // cerr << l << " " << r << " : " << now << endl;
    int left = i, right = n-i-1;
    right = min(left+1, right);
    left = min(left, right);
    int le = i-left, ri = i+right;
    while (l < le) {
      now -= x[l]*2LL;
      ++l;
    }
    // cerr << l << " " << r << " : " << now << endl;
    r = max(r, i);
    while (r < ri) {
      ++r;
      now += (len - x[r]) * 2LL;
    }
    // cerr << l << " " << r << " : " << now << endl;
    // cerr << i << " " << le << " " << ri << " = " << now + x[i] << " " << now << endl;
    ans = max(ans, now + x[i]);
    if (i+1 < n) {
      now += x[i] * 2LL;
      if (r > i)
        now -= (len - x[i+1]) * 2LL;
    }
  }
  // cerr << ans << endl;
  l = r = 0;
  for (int i = 0; i < n; ++i) {
    int left = i, right = n-i-1;
    left = min(left, right+1);
    right = min(left, right);
    int le = i-left, ri = i+right;
    // cerr << l << " " << r << " : " << now << endl;
    while (l < le) {
      now -= x[l]*2LL;
      ++l;
    }
    // cerr << l << " " << r << " : " << now << endl;
    r = max(r, i);
    while (r < ri) {
      ++r;
      now += (len - x[r]) * 2LL;
    }
    // cerr << l << " " << r << " : " << now << endl;
    // cerr << i << " " << le << " " << ri << " = " << now + x[i] << " " << now << endl;
    ans = max(ans, now + len - x[i]);
    if (i+1 < n) {
      now += x[i] * 2LL;
      if (r > i)
        now -= (len - x[i+1]) * 2LL;
    }
  }
  printf("%lld\n", ans);
  return 0;
}

Measured memory_bytes: 1024
/*
 ID: swagchicken1
 PROG:
 LANG: C++11
 */

#include <iostream>
#include <tuple>
#include <cmath>
#include <string>
#include <cstring>
#include <vector>
#include <deque>
#include <queue>
#include <stack>
#include <map>
#include <unordered_map>
#include <set>
#include <algorithm>
#include <vector>
#include <fstream>
#include <iomanip>
#include <ctime>
#include <cctype>
using namespace std;

typedef long long ll;
typedef vector<int> vi;
typedef vector< vector <int> > vvi;
typedef pair<int, int> pii;
typedef pair < pair < int, int >, int > piii;
typedef pair < pair <int, int > , pair <int, int> > piiii;

#define FOR(i,a,b) for(int i = a; i < b; i ++)
#define RFOR(i,a,b) for(int i = a-1; i >= b; i --)

//#define endl '\n';

#define mp make_pair
#define pb push_back
#define f first
#define s second

const int MOD = 1000000007;
double PI = 4*atan(1);


int main() {
    ios_base::sync_with_stdio(0);cin.tie(0);
    //     ofstream fout("trapped.out");
    //      ifstream fin("trapped.in");
   	string str; cin >> str;
  	int y = 0;
    FOR(i,0,4) {
      y += str[i] - '0';
      y *= 10;
    }
    y/= 10;
  int m = 0;
  FOR(i,5,7) {
    m += str[i] -'0';
    m *= 10;
  }
  m/=10;
  int d = 0;
  FOR(i,9,11) {
    d += str[i] - '0';
    d*=10;
  }

  if(y > 2019) {
    cout << "TBD";
  }
  else {
    if(m > 4) {
      cout << "TBD";
    }
    else {
      cout << "Heisei";
    }
  }
 
    
    //  fin.close();
    //    fout.close();
}


Measured memory_bytes: 764
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 1e5 + 10;
bool cmp(pair<int,int> a,pair<int,int> b){
    return a.first < b.first;
}
int main(){
    ios::sync_with_stdio(0);
    cin.tie(0);
    int n;
    cin >> n;
    pair<int,int> p[N];
    for(int i = 1;i <= n; ++i){
        cin >> p[i].first;
        p[i].second = i;
    }
    sort(p+1,p+1+n,cmp);
    for(int i = 1;i <= n; ++i){
        cout << p[i].second <<" ";
    }
    return 0;
}

Measured memory_bytes: 1664
/*
        FFFFFFFFFFFFFFFFFFFFFF
        F::::::::::::::::::::F
        F::::::::::::::::::::F
        FF::::::FFFFFFFFF::::F
          F:::::F       FFFFFF
          F:::::F
          F::::::FFFFFFFFFF
          F:::::::::::::::F
          F:::::::::::::::F
          F::::::FFFFFFFFFF
          F:::::F
          F:::::F
        FF:::::::FF
        F::::::::FF
        F::::::::FF
        FFFFFFFFFFF
*/
 
#include<bits/stdc++.h>
using namespace std;
#define ll long long
//#define int long long
#define mod 1000000007 //998244353
#define fast  ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
#define f(i,n) for(ll i=0;i<n;i++)
#define nl "\n"
#define trace(x) cerr<<#x<<": "<<x<<" "<<endl
#define trace2(x, y) cerr<<#x<<": "<<x<<" | "<<#y<<": "<<y<<endl
#define trace3(x, y, z) cerr<<#x<<":" <<x<<" | "<<#y<<": "<<y<<" | "<<#z<<": "<<z<<endl
#define trace4(a, b, c, d) cerr<<#a<<": "<<a<<" | "<<#b<<": "<<b<<" | "<<#c<<": "<<c<<" | "<<#d<<": "<<d<<endl
#define cout1(a) cout<<a<<nl
#define cout2(a,b) cout<<a<<" "<<b<<nl
#define cout3(a,b,c) cout<<a<<" "<<b<<" "<<c<<nl
#define cout4(a,b,c,d) cout<<a<<" "<<b<<" "<<c<<" "<<d<<nl
#define vcout(v,i) cout<<v[i].fi<<" "<<v[i].se<<nl
#define x first
#define y second
#define mp make_pair
#define pb push_back
#define all(a) (a).begin(), (a).end()
#define ms(v,n) memset((v),n,sizeof(v));
#define pll pair<ll,ll>
#define mll map<ll,ll>
#define sll set<ll>
#define vll vector<ll>
#define vpll vector<pll>
#define maxi(a,b,c) max(a,max(b,c))
#define maxii(a,b,c,d) max(max(a,b),max(c,d))
#define mini(a,b,c) min(a,min(b,c))
#define md(a,b)  ((a%mod)*(b%mod)+mod)%mod 
#define inf 9e18
#define cases ll T;cin>>T;while(T--)
#define BLOCK 500
//const double PI = 3.141592653589793238460;
template<typename T> bool mmax(T &m, const T q) { if (m < q) {m = q; return true;} else return false; }
template<typename T> bool mmin(T &m, const T q) { if (m > q) {m = q; return true;} else return false; }
template<typename T> T add(T &a, T b){ return (a+=b)%=mod; }
template<typename T> T sub(T &a, T b){ (a-=b); if(a<0) a+=mod; }
typedef std::complex<double> Complex;
typedef std::valarray<Complex> CArray;
// const int N=...;

int32_t main()
{
    
	fast
	ll k,n;
	cin>>k>>n;
	
	ll a[n];
	f(i,n)
	cin>>a[i];
	
	if(n==2){
	cout<<min(a[n-1]-a[0],k-(a[n-1]-a[0]));
	return 0;
	}
	ll ans=inf;
	f(i,n-1){
	ans=min(ans,k-(a[i+1]-a[i]));
	}
	
	ans=min(ans,(a[n-1]-a[0]));
	
	
	cout<<ans;
	

	



    

	return 0;
    
}

Measured memory_bytes: 1792
#include <bits/stdc++.h>
using namespace std;


typedef long long ll;
typedef pair<ll,int> P;

struct edge {int to; ll cost; };

int dx[] = {-1, 1, 0,  0};
int dy[] = { 0, 0, 1, -1};
 
int main(){
  ll x;
  cin >> x;
  string s;
  cin >> s;
  ll y = 0;
  for(int i=0; i<4; i++){
    if(i==1) continue;
    y *= 10;
    y += s[i]-'0';
  }
  x *= y;
  x /= 100;
  cout << x << endl;
  return 0;
}


Measured memory_bytes: 3604
#include <iostream>
#include <iomanip>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <array>
#include <tuple>
#include <string>
#include <algorithm>
#include <functional> // greater など
#include <cmath>      // abs など
#include <numeric>    // accumulate, gcd など
#include <cassert>    // assert
#include <stdexcept>
#include <bitset>
using namespace std;

// #include <boost/multiprecision/cpp_int.hpp>
// using namespace boost::multiprecision;

#define rep(i, n) for (int i = 0; i < (int)(n); i++)
typedef long long ll;
// typedef int128_t ll;
// typedef cpp_int ll;

// GCC, 配列のアクセスなど
#define _GLIBCXX_DEBUG
// Clang, 配列のアクセスなど
#define _LIBCPP_DEBUG 0

//////////////// //////////////// //////////////// //////////////// ////////////////
//////////////// //////////////// //////////////// //////////////// ////////////////

// constexpr inline ll mod = 1'000'000'007;
// constexpr int max_n = 200'000 + 5;

int main()
{
    // 高速な入出力
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    cout << setprecision(15); // 浮動小数の表示の精度

    int a,b,c,d; cin>>a>>b>>c>>d;
    while (true) {
        c -= b;
        if (c <= 0) { cout << "Yes\n"; break; }
        a -= d;
        if (a <= 0) { cout << "No\n"; break; }
    }

    return 0;
}


Measured memory_bytes: 3600
#include <iostream>
using namespace std;
int main(void){
    int a,b,c,d,e;
    cin >> a >> b >> c >> d >> e;
    int time=0,temp=a;
    if(a<0){
        for(temp=a;temp<0;temp++){
            time +=c;
        }
        temp=0;
        time +=d;
    }
    for(;temp<b;temp++){
        time += e;
    }
    cout << time <<endl;
}


Measured memory_bytes: 3052
N = gets.to_i
a = gets.split.map(&:to_i)
cnt = [0,0]
2.times do |j|
    sign = -2*j+1
    sum = 0
    N.times do |i|
        sum += a[i]
        if sign == -1 && sum >= 0
            cnt[j] += 1+sum
            sum = -1
        elsif sign == 1 && sum <= 0
            cnt[j] += 1 - sum
            sum = 1
        end
        sign *= -1
    end
end
puts cnt.min



Measured memory_bytes: 8580
#include <bits/stdc++.h>
#define read(n) scanf("%d",&n)
#define write(x) printf("%d",x)
#define F(i,n) for(int i = 0;i < n;i++)
#define ll long long
#define MX 100005
#define MOD 1000000007
#define INF 1000000009
#define set_dp(dp) memset(dp,-1,sizeof(dp))
using namespace std;
int n, m, dp[MX], __next[MX];
char a[MX];
int main(){
	#ifndef ONLINE_JUDGE
	freopen("input.txt","r",stdin);
	freopen("output.txt","w",stdout);
	#endif

	read(n), read(m);

	scanf("%s", a);

	deque<int> q;

	set_dp(dp);
	set_dp(__next);

	if(a[n] == '0'){
		__next[n] = -1;
		dp[n] = 0;
		q.push_back(n);
	}

	for(int i = n - 1; i > -1; i--){
		while(!q.empty() && q.front() - i > m) q.pop_front();
		if(a[i] == '1') continue;
		if(!q.empty()){
			dp[i] = 1 + dp[q.front()];
			__next[i] = q.front();
			while(!q.empty() && dp[q.back()] >= dp[i]) q.pop_back();
			q.push_back(i);
		}
	}

	int j = 0;
	if(__next[j] == -1) cout<<-1;
	while(__next[j] != -1){
		cout<<__next[j] - j<<" ";
		j = __next[j];
	}

	cout<<endl;

	return 0;
}

Measured memory_bytes: 4552
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Arrays;

class Main {
	public static void main(String[] args) throws java.lang.Exception {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		int n = Integer.parseInt(br.readLine());
		String[] arg = br.readLine().split(" ");
		int[] inp = new int[arg.length];
		for (int i = 0; i < arg.length; i++) {
			inp[i] = Integer.parseInt(arg[i]);
		}
		Solver s = new Solver();

		System.out.println(s.solve(inp));
	}

}

class Solver {

	int solve(int[] inp) {

		int max = Arrays.stream(inp).max().getAsInt();
		int min = Arrays.stream(inp).min().getAsInt();
		int sum = 0;
		int temp = 0;
		for (int i = min; i <= max; i++) {
			temp = 0;
			for (int j = 0; j < inp.length; j++) {
				temp += (i - inp[j]) * (i - inp[j]);
			}
			if (sum > temp||sum==0)
				sum = temp;
		}
		return sum;

	}

}

Measured memory_bytes: 28116
#include<bits/stdc++.h>
using namespace std;
int main(){
    int N;
    string S;
    cin>>N>>S;
    int esum = 0;
    for(int i=0;i<N;i++){
        if(S[i] == 'E')esum++;
    }
    int ans = esum;
    for(int i=0;i<N;i++){
        if(S[i] == 'W')++esum;
        else --esum;
        if(esum < ans){
            ans = esum;
        }
    }
    cout << ans << endl;
    return 0;
}

Measured memory_bytes: 900
#include<iostream>
#include<string>
#include<vector>
#include<tuple>
#include<stack>
#include<set>
#include<algorithm>
using namespace std;

#define rep(i, a, b) for (int i = (int)(a); i < (int)(b); ++i)
#define DBG 0
const int INF = 1e9;
int main() {
	while (1) {
		int n;
		cin >> n;
		if (n == 0) break;
		vector<int> t(n), c(n);
		rep(i, 0, n) {
			cin >> t[i] >> c[i];
		}

		set<pair<int, int>> S1, S2;

		{  // init
			rep(i, 0, n) {
				S1.insert({t[i], i});
			}
		}

		int ans = 0;
		while (S1.size() || S2.size()) {
			if (DBG) {
				cout << "S1.size() = " << S1.size() << ", S2.size() = " << S2.size() << endl;
			}

			set<tuple<int, int, bool>> next;
			int a = INF, b = INF;
			if (not S1.empty())a = S1.begin()->first;
			if (not S2.empty())b = S2.begin()->first;
			int T = min(a,b);
			while (!S1.empty()) {
				int x, idx;
				tie(x, idx) = *S1.begin();
				if (x != T) break;
				next.insert(make_tuple(T + t[idx], idx, false));
				S1.erase(S1.begin());
			}
			while (!S2.empty()) {
				int x, idx;
				tie(x, idx) = *S2.begin();
				if (x != T) break;
				next.insert(make_tuple(T + t[idx], idx, true));
				S2.erase(S2.begin());
			}

			for (auto& tpl : next) {
				int nt, idx;
				bool f;
				tie(nt, idx, f) = tpl;
				if (f) {
					// into S1
					if (!S1.empty()) {
						auto itr = S1.rbegin();
						nt = max(nt, get<0>(*itr));
					}

					// decrease c
					c[idx]--;
					if (DBG) {
						cout << "! c ";

						rep(i, 0, n) {
							cout << c[i] << (i == n - 1 ? '\n' : ' ');
						}
					}
					if (c[idx] > 0) {
						S1.insert({ nt, idx });
					}
					else {
						ans = max(ans, nt - t[idx]);  // !!
					}
				}
				else {
					// into S2
					if (!S2.empty()) {
						auto itr = S2.rbegin();
						nt = max(nt, get<0>(*itr));
					}
					S2.insert({ nt, idx });
				}
			}
		}
		cout << ans << endl;
	}
	return 0;
}


Measured memory_bytes: 3100