akhauriyash/Code-Regression huggingface dataset:
import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.StringTokenizer;
import java.io.IOException;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.InputStream;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 *
 * @author AnandOza
 */
public class Main {
    public static void main(String[] args) {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        InputReader in = new InputReader(inputStream);
        PrintWriter out = new PrintWriter(outputStream);
        DABS solver = new DABS();
        solver.solve(1, in, out);
        out.close();
    }

    static class DABS {
        public void solve(int testNumber, InputReader in, PrintWriter out) {
            int n = in.nextInt();
            int first = in.nextInt(), second = in.nextInt();
            int[] a = new int[n + 1];
            for (int i = 0; i < n; i++) {
                a[n - 1 - i] = in.nextInt();
            }
            a[n] = second;

            if (n == 1) {
                out.println(Math.abs(second - a[0]));
                return;
            }

            int[] min = new int[n];
            int[] max = new int[n];
            min[0] = max[0] = Math.abs(a[0] - a[1]);
            for (int i = 1; i < n; i++) {
                min[i] = Math.abs(a[0] - a[i + 1]);
                for (int j = 0; j < i; j++) {
                    min[i] = Math.min(min[i], max[j]);
                }
                max[i] = Math.abs(a[0] - a[i + 1]);
                for (int j = 0; j < i; j++) {
                    max[i] = Math.max(max[i], min[j]);
                }
            }

            int answer = max[n - 1];
            out.println(answer);
        }

    }

    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());
        }

    }
}



Measured memory_bytes: 23892
#include <bits/stdc++.h>
#define rep(i,n) for(long long int i = 0; i < n; i++)
#define _rep(i,m,n) for(long long int i = m; i < n; i++)
#define print(n) std::cout << n << std::endl
#define _print(n) std::cout << n
using namespace std;
typedef long long ll;
const int N = 1000000;
const ll mod = 1000000007;
using Graph = vector<vector<int>>;
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};

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

   int count = 0;
   for(int i = 0; i < n; i++){
      while(i < n - 1 && a[i] == a[i + 1])i++;
      if(a[i] > a[i + 1]){
         while(i < n - 1 && a[i] >= a[i + 1])i++;
      }else if(a[i] < a[i + 1]){
         while(i < n - 1 && a[i] <= a[i + 1])i++;
      }
      count++;
   }
   cout << count << endl;
}

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

int dp[1005][1005];

int main(){
    int q;
    cin>>q;
    while(q--){
        string s,t;
        cin>>s>>t;
        for(int i=0; i<1005; ++i){
            for(int j=0; j<1005; ++j){
                dp[i][j]=0;
            }
        }
        for(int i=0; i<s.size(); ++i){
            for(int j=0; j<t.size(); ++j){
                if(s[i]==t[j]){
                    dp[i+1][j+1]=max(dp[i+1][j+1],dp[i][j]+1);
                }
                dp[i+1][j+1]=max(dp[i+1][j+1],dp[i][j+1]);
                dp[i+1][j+1]=max(dp[i+1][j+1],dp[i+1][j]);
            }
        }
        cout<<dp[s.size()][t.size()]<<endl;
    }
}


Measured memory_bytes: 7128
#include<iomanip>
#include <iostream>

using namespace std;

double printmax(int n,double a[])
{
    double arr[n+1][n+1];
    arr[0][0]=1.0;
    for(int i=1;i<=n;i++)
    {
        arr[0][i]=0.0;
    }
    for(int i=1;i<=n;i++)
    {
        for(int j=0;j<=n;j++)
        {
            if(j>i)
            {
                arr[i][j]=0.0;
            }
            if(j==0)
            {
                arr[i][j]=arr[i-1][j]*(1.0-a[i]);
            }
            else
                arr[i][j]=arr[i-1][j]*(1.0-a[i])+arr[i-1][j-1]*a[i];
                //cout<<arr[i-1][j]*(1.0-a[i-1])<<"and"<<arr[i-1][j-1]*a[i]<<endl;
        }
    }
    double res=0.0;
    /*for(int i=0;i<=n;i++)
    {
        for(int j=0;j<=n;j++)
        {
            cout<<arr[i][j]<<" ";
        }
        cout<<endl;
    }*/
    for(int i=(n+1)/2;i<n;i++)
    {
        //cout<<i<<" "<<arr[n][i]<<endl;
        res=res+arr[n][i];
    }
    return res;
}
int main()
{
    int n;
    cin>>n;
    double arr[n+1];
    arr[0]=0.0;
    for(int i=1;i<n+1;i++)
    {
        cin>>arr[i];
    }
    cout<<setprecision(20)<<fixed;
    cout<<printmax(n+1,arr);
    return 0;
}


Measured memory_bytes: 70656
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n)  for(long long i=0;i<(long long)(n);i++)
#define REP(i,k,n) for(long long i=k;i<(long long)(n);i++)
#define all(a) a.begin(),a.end()
#define eb emplace_back
#define pb push_back
#define lb(v,k) (lower_bound(all(v),k)-v.begin())
#define chmin(x,y) if(x>y)x=y
#define chmax(x,y) if(x<y)x=y
typedef long long ll;
typedef pair<ll,ll> P;
typedef tuple<ll,ll,ll> PP;
typedef priority_queue<ll> PQ;
typedef priority_queue<ll,vector<ll>,greater<ll>> SPQ;
using vi=vector<ll>;
using vvi=vector<vector<ll>>;
using vc=vector<char>;
using vvc=vector<vector<char>>;
const ll inf=100100100100;
const int Inf=1001001001;
const int mod=1000000007;
int main(){
    vi v(5);
    rep(i,5)cin>>v[i];
    int a=0;
    int sum=0;
    rep(i,5){
        sum+=v[i];
        sum+=(10000-v[i])%10;
        chmax(a,(10000-v[i])%10);
    }
    cout<<sum-a<<endl;
}

Measured memory_bytes: 256
#include<bits/stdc++.h>
typedef long long ll;
using namespace std;
int main()
{
  
       ll x,y;
       cin>>x>>y;
       int i=0;
       int flag=1;
       while(true)
       {

           if((((x-i)*2+i*4==y)||(i*2+(x-i)*4==y)))
               {
                   flag=0;
                   break;
               }
               else
                i++;
                if(i>x)
                    break;

       }
       if(flag==1)
        cout<<"No"<<endl;
       else
        cout<<"Yes"<<endl;
  
 return 0;
}



Measured memory_bytes: 3636
#include <iostream> // cout, endl, cin
#include <string> // string, to_string, stoi
#include <vector> // vector
#include <algorithm> // min, max, swap, sort, reverse, lower_bound, upper_bound
#include <utility> // pair, make_pair
#include <tuple> // tuple, make_tuple
#include <cstdint> // int64_t, int*_t
#include <cstdio> // printf
#include <map> // map
#include <queue> // queue, priority_queue
#include <set> // set
#include <stack> // stack
#include <deque> // deque
#include <unordered_map> // unordered_map
#include <unordered_set> // unordered_set
#include <bitset> // bitset
#include <cmath>

using namespace std;

#define ll long long
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)

int main() {
	ll A, B, K;
	cin >> A >> B >> K;

	ll tmp = K - A;
	A = max(A - K, 0LL);
	K = max(tmp, 0LL);

	B = max(B - K, 0LL);

	cout << A << " " << B << endl;
}

Measured memory_bytes: 256
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int m = sc.nextInt();
        long[] brokens = new long[n+1];
        long[] ins = new long[n+1];
        for(int i = 0;i < m;i++){
            brokens[sc.nextInt()] = 1;
        }
        for(int i = 1;i < n + 1;i++){
            if(brokens[i] == 1){
                ins[i] = 0;
            }else if(i == 1){
                ins[i] = 1;
            }else if(i == 2){
                ins[i] = ins[i - 1] + 1;
            }else{
                ins[i] = ins[i - 1] + ins[i - 2];
                ins[i] = ins[i] % 1000000007;
            }
        }
        System.out.println(ins[n]);
    }
}

Measured memory_bytes: 47040
#include <iostream>
#include <iomanip>
#include <string>
#include <sstream>
#include <fstream>
#include <cassert>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <utility>
#include <vector>
#include <queue>
#include <map>
#include <set>
#include <unordered_map>
#include <unordered_set>
#include <bitset>
#include <complex>

using namespace std;

using ll = long long;
using ld = long double;
using pii = pair<int, int>;

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

    int n, m;
    scanf("%d %d", &n, &m);
    printf("%lld\n", (1 << m) * (100L * (n - m) + 1900L * m));
    
    return 0;
}


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

int main(){
	int n;
	cin>>n;
	vector<int> imo(100003,0);
	for(int i=0;i<n;i++){
		int a,b;
		cin>>a>>b;
		imo[a]++;
		imo[b+1]--;
	}
	
	int ans=0;
	int sum=0;
	
	for(int i=0;i<100003;i++){
		sum+=imo[i];
		if(sum+1==i){
			ans=max(ans,sum);
		}else if(sum+1>i){
			ans=max(ans,i-1);
		}
	}
	
	cout<<ans<<endl;
	return 0;
}

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

using ll = long long;

int main() {
    int n; cin >> n;
    stack<int> st;
    for(int i = 0; i < n; ++i) {
        int a; cin >> a;
        st.push(a);
    }

    int ans = 0;
    while(!st.empty()) {
        int x = st.top(); st.pop();
        int cnt = 1;
        while(!st.empty() && st.top() == x) {
            cnt++;
            st.pop();
        }
        ans += cnt / 2;
    }
    cout << ans << endl;
}


Measured memory_bytes: 256
#include <iostream>
#include <vector>
using namespace std;

int main(){
    long long n, t; cin >> n >> t;
    vector<long long> times(n+1); for (long long i=0;i<n;i++){cin >> times[i];}
    times[n] = times[n-1] + t;
    long long acc = 0;
    for (long long i=1;i<n+1;i++){
        if ((times[i] - times[i-1]) > t){
            acc += t;
        } else {
            acc += times[i] - times[i-1];
        }
    }
    cout << acc << endl;
    return 0;
}

Measured memory_bytes: 1792
package main

import "fmt"

func main() {
	var k, s int
	fmt.Scanf("%d %d", &k, &s)
	var count = 0
	for x := 0; x <= k; x++ {
		for y := 0; y <= k; y++ {
			z := s - x - y
			if 0 <= z && z <= k {
				count++
			}
		}
	}
	fmt.Printf("%d", count)
}


Measured memory_bytes: 1844
use proconio::input;
use proconio::marker::Chars;

fn main() {
    input! {
        h: usize,
        w: usize,
        grid: [Chars; h],
    }

    let mov = [(1, 0), (0, 1), (-1, 0), (0, -1)];

    for y in 0..h {
        for x in 0..w {
            if grid[y][x] == '.' {
                continue;
            }
            let mut isolate = true;
            for &mo in &mov {
                let next_x = x as isize + mo.0;
                let next_y = y as isize + mo.1;

                if next_x < 0 || next_x >= w as isize || next_y < 0 || next_y >= h as isize {
                    continue;
                }

                let next_x = next_x as usize;
                let next_y = next_y as usize;

                if grid[next_y][next_x] == '#' {
                    isolate = false;
                }
            }
            if isolate {
                println!("No");
                return;
            }
        }
    }
    println!("Yes");
}


Measured memory_bytes: 2176
#include<iostream>
#include<stdio.h>
#include<string>
#include<vector>
#include<algorithm>
#include<cmath>
using namespace std;

int main() {
    int N;
    cin >> N;
    vector<int>A(N);
    for (int i = 0; i < N; i++) {
        cin >> A[i];
    }

    sort(A.begin(), A.end());
    int min = A[0];
    while (1) {
        for (int i = 1; i < N; i++) {
            if (A[i] % min == 0) { 
                A.erase(A.begin() + i); 
                i--;
                N--;
            }
            else {
                A[i] %=min;
            }
        }
        sort(A.begin(), A.end());
        if (A[0] == min)break;
        else if (A[0] < min)min = A[0];
    }

    cout << min << "\n";

}

Measured memory_bytes: 640
#include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>
#include <string>
#include <iomanip>
#include <set>
#include <map>

using namespace std;

int main(){
    int N,M;
    cin >> N >> M;
    
    vector<int> A;
    A.push_back(0);
    for(int i=0;i<N;i++){
        int y;cin >> y;
        A.push_back((y+A[i])%M);
    }
    A.push_back(M+2);
    sort(A.begin(),A.end());
    
    long long int ans=0;
    
    int memv=0;
    long long int kazu=0;
    for(int i=0;i<=N+1;i++){
        if(memv==A[i]){
            kazu++;
        }else{
            ans += kazu*(kazu-1)/2;
            kazu=1;
            memv=A[i];
        }
    }
    
    cout << ans;
    return 0;
}










Measured memory_bytes: 892
#include <bits/stdc++.h>
#define rep(i,n) for (int i = 0; i < (int)(n); i++)
#define REP(i,n) for (int i = 1; i < (int)(n); i++)
#define all(x) x.begin(),x.end()
#define rall(x) x.rbegin(),x.rend()
#define vout(x) rep(i,x.size()) cout << x[i] << " "
template<class T> bool chmin(T &a, T b) {if(a>b) {a=b;return 1;}return 0;}
template<class T> bool chmax(T &a, T b) {if(a<b) {a=b;return 1;}return 0;}
using namespace std;
using vint = vector<int>;
using vvint = vector<vector<int>>;
using ll = long long;
using vll = vector<ll>;
using vvll = vector<vector<ll>>;
using P = pair<int,int>;
const int inf = 1e9;
const ll inf_l = 1e18;
const int MAX = 1e5;

const int mod = 1e9+7;
struct mint {
    ll x;
    mint (ll x = 0) : x((x%mod+mod)%mod) {}
    mint operator -() const {return mint(-x);}
    mint& operator +=(const mint a) {
        if ((x += a.x) >= mod) x -= mod;
        return *this;
    }
    mint& operator -=(const mint a) {
        if ((x += mod-a.x) >= mod) x -= mod;
        return *this;
    }
    mint& operator *=(const mint a) {(x *= a.x) %= mod; return *this;}
    mint operator +(const mint a) const {return mint(*this) += a;}
    mint operator -(const mint a) const {return mint(*this) -= a;}
    mint operator *(const mint a) const {return mint(*this) *= a;}
    mint pow(ll t) const {
        if (!t) return 1;
        mint a = pow(t>>1);
        a *= a;
        if (t&1) a *= *this;
        return a;
    }
    mint inv() const {return pow(mod-2);}
    mint operator /=(const mint a) {return *this *= a.inv();}
    mint operator /(const mint a) {return mint(*this) /= a;}
};
istream& operator >>(istream &is, const mint &a) {return is >> a.x;}
ostream& operator <<(ostream &os, const mint &a) {return os << a.x;}

mint dp[105][100005];

int main() {
    int n, k; cin >> n >> k;
    vint a(n);
    rep(i,n) cin >> a[i];
    dp[0][0] = 1;
    vector<mint> sum(k+2,1);
    sum[0] = 0;
    REP(i,n+1) {
        vector<mint> next_sum(k+2);
        next_sum[0] = 0;
        rep(j,k+1) {
            dp[i][j] = sum[j+1] - sum[max(0,j-a[i-1])];
            next_sum[j+1] = next_sum[j] + dp[i][j];
        }
        swap(next_sum,sum);
    }
    cout << dp[n][k] << endl;
}

Measured memory_bytes: 83840
//#include "stdafx.h"
#include <bits/stdc++.h>
#define ll long long
#define vi vector<int>
#define e "\n"
#define MaRiaMa { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); }
#define fix(n) cout << fixed << setprecision(n);
using namespace std;
int main()
{
	MaRiaMa;
	int a, b, c;
	cin >> a >> b >> c;
	multiset<int> p;
	p.insert(a); p.insert(b); p.insert(c);
	int n= max({ p.count(a), p.count(b), p.count(c) });
	if (n == 1)
		cout << "3";
	else if (n == 3)
		cout << "1";
	else
		cout << n;
}

Measured memory_bytes: 3604
#include <iostream>
#include <string>
#include <algorithm>
#include <vector>
#include <iomanip>
#include <cmath>
#include <queue>
#include <stack>
#include <cstdlib>
#include <map>
#include <iomanip>
#include <set>
using namespace std;
#define pie 3.141592653589793238462643383279
#define mod 1000000007
#define INF 1145141919364364
#define int long long
#define all(vec) vec.begin(),vec.end()
#define P pair<int,int>
#define S second
#define F first
int gcd(int x, int y) {
	if (y == 0)return x;
	return gcd(y, x%y);
}
int lcm(int x, int y) {
	return x / gcd(x, y)*y;
}
bool prime(int x) {
	for (int i = 2; i <= sqrt(x); i++) {
		if (x%i == 0)return false;
	}
	return true;
}
int kai(int x, int y) {
	int res = 1;
	for (int i = x - y + 1; i <= x; i++) {
		res *= i; res %= mod;
	}
	return res;
}
int mod_pow(int x, int y, int m) {
	int res = 1;
	while (y > 0) {
		if (y & 1) {
			res = res * x % m;
		}
		x = x * x % m;
		y >>= 1;
	}
	return res;
}
int comb(int x, int y) {
	if (y > x)return 0;
	return kai(x, y) * mod_pow(kai(y, y), mod - 2, mod) % mod;
}

string s;

signed main() {
	cin >> s;
	if (s == "SUN")cout << 7 << endl;
	else if (s == "MON")cout << 6 << endl;
	else if (s == "TUE")cout << 5 << endl;
	else if (s == "WED")cout << 4 << endl;
	else if (s == "THU")cout << 3 << endl;
	else if (s == "FRI")cout << 2 << endl;
	else cout << 1 << endl;
}

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

int main()
{
    int n;
    cin >> n;
    vector<long> h(n);
    for(int i=0;i<n;i++) cin >> h[i];
    int ans = 0;
    int tmp = 0;
    for(int i=0;i<n-1;i++){
        if(h[i]>=h[i+1]){
            tmp++;
            ans = max(ans,tmp);
        }
        else tmp = 0;
    }
    cout << ans << endl;
    return 0;
}

Measured memory_bytes: 1024
#include<bits/stdc++.h>
using namespace std;
typedef unsigned long long ull;
typedef long long ll;
const int N=1e6+10;
map<int,int> mp;

int main(){
	int i,j,k,n;
	cin>>n;
	for(i=1;i<=n;i++){
		cin>>k;
		if(mp[k]==0)mp[k]=1;
		else mp[k]=0;
	}
	j=0;
	for(map<int,int>::iterator it=mp.begin();it!=mp.end();it++){
		if(it->second==1) j++;
	}
	cout<<j;
	return 0;
}


Measured memory_bytes: 4992
#include <iterator>
#include <list>
#include <algorithm>
#include <iostream>
#include <unordered_set>
#include <unordered_map>
#include <vector>
#include <sstream>
#include <iomanip>
#include <string>
#include <cmath>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <numeric>
using namespace std;

int main() {
  int N; cin >> N;
  vector<int> d(N);
  for (int i = 0; i < N; i++) {
    cin >> d[i];
  }

  int ans = 0;
  for (int i = 0; i < N; i++) {
    for (int j = i + 1; j < N; j++) {
      ans += d[i] * d[j];
    }
  }

  cout << ans << endl;

  return 0;
}


Measured memory_bytes: 256
// ABC147B.cpp
//#include "pch.h"
#include <iostream>
#include<string>
using namespace std;
int main()
{
	string S;
	cin >> S;

	int ans = 0;
	for (int i = 0; i < S.length(); i++)
	{
		if (S.at(i)!=S.at(S.length()-1-i))
		{
			ans++;
		}
	}
	cout << ans/2 << endl;
}

Measured memory_bytes: 256
//https://atcoder.jp/contests/abc043/tasks/abc043_b

import java.io.*;
import java.util.ArrayList;

class Main{
    public static void main(String[] args) throws IOException{
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String S = br.readLine();
        ArrayList<String> s = new ArrayList<>();
        for (int i=0;i<S.length();i++){
            if (String.valueOf(S.charAt(i)).equals("B")){
                if (s.size()!=0){
                    s.remove(s.size()-1);
                } else {
                    s.clear();
                }
            } else {
                s.add(String.valueOf(S.charAt(i)));
            }
        }
        String ans = String.join("", s);
        System.out.println(ans);
    }
}

Measured memory_bytes: 21076
#include <stdio.h>
#include <math.h>
#define N 33000
 
int main()
{
  int i,j,n;
  int prime[N];
  for (i=0;i<N;i++){
    prime[i]=1;
  }
  prime[0]=0;
  for (i=1;i<(int)sqrt(N);i++){
    if (prime[i]==1){
      for (j=i+1;(i+1)*j<=N;j++){
        prime[(i+1)*j-1]=0;
      }
    }
  }
  while(scanf("%d",&n)){
    if (n==0)
      break;
    int cnt=0;
    for (i=1;i<n;i++){
      if (i>=n/2)
        break;
      if (prime[i]==1)
        if (prime[n-(i+2)]==1)
          cnt++;
    }
    printf("%d\n",cnt);
  }
  return 0;
}

Measured memory_bytes: 728
n = int(input())
d = list(map(int, input().split()))
d.sort()
num = 0
n3 = n//2
n2 = n3-1
if d[n2] == d[n3]:
    print(0)
else:
    for i in range(d[n2],d[n3]):
        num += 1
    print(num)

Measured memory_bytes: 20632
import os, sys, re, math

N = int(input())
A = list(map(int,input().split(' ')))

while True:
    min_a = min(A)
    for i in range(len(A)):
        if A[i] % min_a > 0:
            A[i] = A[i] % min_a
        else:
            A[i] = min_a

    if len(set(A)) == 1:
        break

print(min_a)


Measured memory_bytes: 14508
#define _CRT_SECURE_NO_WARNINGS
#define _USE_MATH_DEFINES
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <algorithm>
#include <string>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <functional>
#include <utility>
#include <tuple>
#include <cctype>
#include <bitset>
#include <complex>
#include <cmath>
using namespace std;
//#define INF 0x3f3f3f3f
#define INFLL 0x3f3f3f3f3f3f3f3fLL
#define MOD 1000000007
#define mp make_pair
#define mt make_tuple
#define pb push_back
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pint;
typedef pair<ll,ll> pll;
typedef tuple<int,int,int> tint;
typedef vector<int> vint;
typedef vector<ll> vll;
typedef vector<pint> vpint;
int dx[8]={0,0,-1,1,1,1,-1,-1};
int dy[8]={-1,1,0,0,1,-1,1,-1};
const int SIZE=100;
//ここまでテンプレ
int main(){
	ll N;
	cin>>N;
	vll A,B;
	ll sum=0;
	for(int i=0;i<N;i++){
		int a,b;
		cin>>a>>b;
		A.pb(a);
		B.pb(b);
		sum+=a;
	}
	vll alpha,beta;
	for(int i=0;i<N;i++){
		if(A[i]>B[i]){
			alpha.pb(A[i]);
			beta.pb(B[i]);
		}
	}
	if(alpha.size()==0){
		cout<<0<<endl;
		return 0;
	}
	ll mini=INFLL;
	for(int i=0;i<beta.size();i++){
		mini=min(mini,beta[i]);
	}
	cout<<sum-mini<<endl;
	return 0;
}

Measured memory_bytes: 892
#include <iostream>
#include <sstream>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <vector>
#include <map>
#include <queue>
#include <stack>
#include <algorithm>
#include <utility>
#include <functional>
#include <cmath>

#define REP(i,x,y) for(int i=(int)(x);i<(int)(y);i++)
#define RREP(i,x,y) for(int i=(int)(x);i>(int)(y);i--)
#define all(x) (x).begin(),(x).end()

#ifdef LOCAL
#define eprintf(...) fprintf(stderr, __VA_ARGS__)
#else
#define eprintf(...) 42
#endif

typedef long long ll;

using namespace std;

int X;

int solve() {
    int x = 0;
    int ans = 0;
    for(int i=0; x != X; i++){
        x += i;
        if(x >= X) {
            ans = i;
            break;
        }
    }
    return ans;
}

int main() {
    // to shorten execution time for iostream
    cin.tie(0);
    ios::sync_with_stdio(false);

    cin >> X;
    cout << solve() << "\n";
    return 0;
}


Measured memory_bytes: 256
#include <iostream>
#include <cmath>
#include <vector>
#include <string>
#include <algorithm>

using namespace std;

#define ll long long
#define vi vector<int>
#define rep(i,n) for(int i=0, i##_len=(n); i<i##_len; ++i)

int A,B;

void solve(){
    if(A <= B)
        cout << A << endl;
    else
        cout << A-1 << endl;
}

int main(){
    cin >> A >> B;

    solve();
}


Measured memory_bytes: 256
#include <bits/stdc++.h>
using namespace std;
 
int main() {
  long n,kosuu,k,ans;
  cin>>n;
  ans=1;
  vector<long> vec(n);
  for(long i=0; i<n; i++){
    vec.at(i)=0;
  }
  for(long i=0; i<n; i++){
    cin>>k;
    if(i==0 && k>1){
      ans=0;
    }
    vec.at(k)=vec.at(k)+1;
  }
  kosuu=0;
  for(long i=1; i<n; i++){
    if(vec.at(i)>0){
      k=vec.at(i);
      kosuu=kosuu+k;
      while(k>0){
        ans=(ans*vec.at(i-1))%998244353;
        k=k-1;
      }
    }
    else{
      break;
    }
  }
  if(kosuu+1==n && vec.at(0)==1){
    cout<<ans;
  }
  else{
    ans=0;
    cout<<ans;
  }
}

Measured memory_bytes: 1024
H,W = map(int, input().split())
area = [list(input()) for i in range(H)]

def calcDist(startIdx, area):
     que = [startIdx]
     dist = [[-1 for i in range(W)] for i in range(H)]
     dist[startIdx[0]][startIdx[1]] = 0
     
     while len(que) != 0:
         idx = que.pop(0)
         
         if idx[0]-1 >= 0 and area[idx[0]-1][idx[1]] != '#' and dist[idx[0]-1][idx[1]] == -1:
             que.append([idx[0]-1, idx[1]])
             dist[idx[0]-1][idx[1]] = dist[idx[0]][idx[1]] + 1
         
         if idx[0]+1 < H and area[idx[0]+1][idx[1]] != '#' and dist[idx[0]+1][idx[1]] == -1:
             que.append([idx[0]+1, idx[1]])
             dist[idx[0]+1][idx[1]] = dist[idx[0]][idx[1]] + 1
             
         if idx[1]-1 >= 0 and area[idx[0]][idx[1]-1] != '#' and dist[idx[0]][idx[1]-1] == -1:
             que.append([idx[0], idx[1]-1])
             dist[idx[0]][idx[1]-1] = dist[idx[0]][idx[1]] + 1
             
         if idx[1]+1 < W and area[idx[0]][idx[1]+1] != '#' and dist[idx[0]][idx[1]+1] == -1:
             que.append([idx[0], idx[1]+1])
             dist[idx[0]][idx[1]+1] = dist[idx[0]][idx[1]] + 1
     
     return dist
     
def searchWhiteNum(area):
    ans = 0
    for i in range(H):
        for j in range(W):
            if area[i][j] == '.':
                ans += 1
    
    return ans

dist = calcDist([0,0], area)
neededWhiteNum = dist[H-1][W-1] + 1
ans = searchWhiteNum(area)
if neededWhiteNum == 0:
    print(-1)
else:
    print(ans-neededWhiteNum)

Measured memory_bytes: 3192
import java.util.*;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int x = sc.nextInt();
        int y = sc.nextInt();
        int z = sc.nextInt();
        int k = sc.nextInt();
        long[] xs = new long[x];
        long[] ys = new long[y];
        long[] zs = new long[z];
        for (int i = 0; i < x; i++) {
            xs[i] = sc.nextLong();
        }
        for (int j = 0; j < y; j++) {
            ys[j] = sc.nextLong();
        }
        for (int l = 0; l < z; l++) {
            zs[l] = sc.nextLong();
        }
        PriorityQueue<Long> pq = new PriorityQueue<>();
        for (int i = 0; i < x; i++) {
            for (int j = 0; j < y; j++) {
                long num = xs[i] + ys[j];
                if(pq.size() < k) {
                    pq.add(num);
                } else if(pq.peek() < num) {
                    pq.poll();
                    pq.add(num);
                }
            }
        }
        PriorityQueue<Long> pq2 = new PriorityQueue<>();
        while(!pq.isEmpty()) {
            long pqNum = pq.poll();
            for (int j = 0; j < z; j++) {
                long num = pqNum + zs[j];
                if(pq2.size() < k) {
                    pq2.add(num);
                } else if(pq2.peek() < num) {
                    pq2.poll();
                    pq2.add(num);
                }
            }
        }
        String answer = "";
        while(!pq2.isEmpty()) {
            answer = (pq2.poll() + "\n") + answer;
        }
        System.out.print(answer);
    }
}


Measured memory_bytes: 92120
#include<iostream>
#include<vector>
using namespace std;

int n;

bool is_even(vector<int> a){
    for(int i = 0; i < n; i++){
        if(a[i] % 2 == 1) return false;
    }
    return true;
}

int main(){
    cin >> n;
    vector<int> a(n,0);
    for(int i = 0; i < n; i++) cin >> a[i];

    int count = 0;

    while(is_even(a)){
        for(int i = 0; i < n; i++) a[i] = a[i] / 2;
        count++;
    }

    cout << count << endl;

    return 0;
}

Measured memory_bytes: 256
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
using namespace std;

#define rep(i,n) for(int i=0;i<(n);i++)
constexpr int INF = (1<<29);
using ll = long long;
using pii = pair<int,int>;

struct edge{
    int to,cost;
    edge(int to, int cost) : to(to), cost(cost) {}
};

int main(){
    int k; cin >> k;
    vector<vector<edge>> g(k);

    for(int i=1;i<k;i++){
        g[i].emplace_back((i+1)%k,1);
        g[i].emplace_back((i*10)%k,0);
    }

    vector<int> d(k,INF);
    priority_queue<pii,vector<pii>,greater<pii>> que;
    que.push(pii(1,1));
    d[1] = 1;
    while(!que.empty()){
        pii now = que.top(); que.pop();
        int v = now.second;
        if(d[v] < now.first) continue;
        for(edge& e : g[v]){
            if(d[e.to] > d[v] + e.cost){
                d[e.to] = d[v] + e.cost;
                que.push(pii(d[e.to],e.to));
            }
        }
    }

    cout << d[0] << endl;
}


Measured memory_bytes: 6780
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double lld;

#define forn(i,n) for(ll i = 0;i<n;i++)
#define forne(i,j,n) for(ll i = j;i<n;i++)
const ll mod = 1e9+7;
const ll mod2 = 998244353;
const ll INF = 1000000001;
const ll N = 100001;
const lld PI = 3.141593;
#define F first
#define pb(a) push_back(a)
#define S second
#define pii pair<int,int>
typedef pair<ll,ll> pll;

ll powm(ll base,ll exp) {
	ll ans=1;
	while(exp){
		if(exp&1) ans=(ans*base)%mod;
		exp>>=1,base=(base*base)%mod;
	}
	return ans;
}

// ll dir[4][2] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};
// bool valid(ll i, ll j) {
// 	return i >= 1 && i <= n && j >= 1 && j <= m; 
// }
// int maxdepth = INT_MAX;
// vector<int> edgeVector[100000];
// ll minans = 1;
// printf("1 %d D\n",abs(r));
// priority_queue<ll,vector<ll>,greater<ll>> pq;
// ll a[N],b[N];
// vector<vector<ll>> adj(N);
// vector<set<ll>> adjs(N);
// ll adj[1001][1001];
// vector<vector<ll>> ans(1001);
// bool visited[N];
// ll cnt[1001];
// ll h,c,t;
// vector<pair<int, pair<int, int> > > g[300043];

ll d[10000001];

int main(){
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output1.txt", "w", stdout);
#endif
ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);

forne(i,1,1e7+1){
	ll k = 1;
	while(i*k <= 1e7+1){
		d[i*k]++;
		k++;
	}
}
// forn(i,5) cout<<d[i]<<" ";
// cout<<endl;
ll n;
cin>>n;
ll fa = 0;
forne(i,1,n+1){
	fa+=(i*d[i]);
}
cout<<fa<<endl;

}

Measured memory_bytes: 81744
#include <stdio.h>
#include <iostream>
#define int long long

using namespace std;

const int N=123456;

int a[N];
int b[N];

signed main() {
    int n,k;
    int gh;
    cin>>n>>k;
    for(int i=1;i<=n;i++) {
        cin>>gh;
        a[i]=gh;
        a[i]+=a[i-1];
        if(gh>=0) b[i]=b[i-1]+gh;
        else b[i]=b[i-1];
    }
    int ans=0;
    for(int i=1;i<=n-k+1;i++) {
        int foo=a[k+i-1]-a[i-1];
        foo=max(foo,0ll);
        ans=max(ans,foo+b[i-1]+b[n]-b[k+i-1]);
    }
    cout<<ans<<endl;
    return 0;
}


Measured memory_bytes: 1792
#include <bits/stdc++.h>
#define FOR(i, a, b) for(int i = (a);i < (b);++i)
#define FORR(i, b, a) for (int i = (b) - 1;i >= 0;--i)
#define REP(i, n) for(int i = 0;i < (n);++i)
#define REPR(i, n) for(int i = (n) - 1;i >= 0;--i)
#define ITER(itr, v) for(auto itr = v.begin();itr != v.end();++itr)
#define SORT(v) sort(v.begin(), v.end())
#define REV(v) reverse(v.begin(), v.end())
#define SIZE(v) int(v.size())
using namespace std;
typedef long long ll;
typedef pair<int, int> P;



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

	int K;
	cin >> K;
	queue<ll> qu;
	FOR(i, 1, 10) qu.emplace(i);
	REP(i, K) {
		ll n = qu.front();
		qu.pop();
		if (i == K - 1) {
			cout << n << endl;
			return 0;
		}
		if (n % 10) qu.emplace(n * 10 + n % 10 - 1);
		qu.emplace(n * 10 + n % 10);
		if (n % 10 < 9) qu.emplace(n * 10 + n % 10 + 1);
	}
	
	return 0;
}


Measured memory_bytes: 1792
import Data.Array
import Control.Monad

gen_all_pat::Int->[Array Int Bool]
gen_all_pat n = let rec_gen i = if i == 0 then [[]] else concat [ [(False:ls),(True:ls)] | ls <- rec_gen (i-1)] in
                (map (listArray (1,n))) (rec_gen n)

gen_checker::(Int,[Int]) -> Array Int Bool -> Bool
gen_checker (p,ls) ar = (mod (length (filter id [ar!i|i<-ls])) 2) == p

solver::Int -> [(Int,[Int])] -> Int
solver n dat = let all_pat = gen_all_pat n  in
               (length.filter (\ ar -> and [gen_checker d ar|d<-dat] )) all_pat

main::IO()
main=do
  n:m:_<- return .map read. words=<< getLine
  dat_r<- forM [1..m] (\_-> return.tail.map read.words=<<getLine)
  dat_l<- return.map read.words=<<getLine
  print (solver n (zip dat_l dat_r))


Measured memory_bytes: 892
#include <iostream>
#include<string>
#include<cmath>
#include<algorithm>
#include<cctype>
#include<queue>
#include<regex>
#include<stack>
#include<stdio.h>
#include<vector>
#include<set>
#include<map>
#include<iomanip>
#define rep(i,n) for(int i=0;i<n;i++)
typedef int long long ll;
using namespace std;
int dx[4]={1,0,-1,0};
int dy[4]={0,1,0,-1};

int main(){
    
    int n;
    cin>>n;
    int l[n];
    rep(i,n){
        cin>>l[i];
    }
    sort(l,l+n);
    reverse(l,l+n);
    int x=0;
    for(int i=1;i<n;i++){
        x+=l[i];
    }
    if(x>l[0]){
        cout<<"Yes"<<endl;
    }else{
        cout<<"No"<<endl;
    }
return 0;
}


Measured memory_bytes: 256
#include<bits/stdc++.h>
using namespace std;
int main()
{
  int A,B,C,D;
  cin >> A >> B >> C >> D;
  
  int L,R;
  L = A + B;
  R = C + D;
  
  if (L>R)
    cout << "Left" << endl;
  
  if (L==R)
    cout << "Balanced" << endl;
  
  if (L<R)
    cout << "Right" << endl;
  
}


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

#define pb push_back
#define eb emplace_back
#define rep(i,a,b) for(auto i=(a);i!=(b);++i)
#define all(v) (v).begin(), (v).end()
#define rs resize
#define x first
#define y second

using namespace std;

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<int> vi;

ostream& operator<<(ostream &out, const pii &a) { return out << '(' << a.x << ", " << a.y << ')'; }
istream& operator>>(istream &in, pii &a) { return in >> a.x >> a.y; }

const int INF = 2147483647;
const ll LLINF = 9223372036854775807LL;

// lambda-expression: [] (args) -> retType { body }

void run()
{
	int N, M;
	cin >> N;
	vi A(N); rep(i,0,N) cin >> A[i];
	cin >> M;
	vi B(M); rep(i,0,M) cin >> B[i];
	sort(all(A)); sort(all(B));

	int ptrA = 0, ptrB = 0;
	while (ptrB < M) {
		while (ptrA < N && A[ptrA] < B[ptrB]) ptrA++;
		if (ptrA >= N || A[ptrA] > B[ptrB]) {
			cout << "NO" << endl; return;
		}
		ptrA++;
		ptrB++;
	}
	cout << "YES" << endl;
	return;
}

int main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);
	// cerr << boolalpha;
	(cout << fixed).precision(10);
	run();
	return 0;
}

Measured memory_bytes: 1792
#
# Written by NoKnowledgeGG @YlePhan
# ('ω')
#
#import math
#mod = 10**9+7
#import itertools
#import fractions
#import numpy as np
#mod = 10**4 + 7
"""def kiri(n,m):
  r_ = n / m
  if (r_ - (n // m)) > 0:
    return (n//m) + 1
  else:
    return (n//m)"""
 
def main():
  n=int(input())
  v=list(map(int,input().split()))
  c=list(map(int,input().split()))
  x=[0]*n
  for i in range(n):
    x[i]=max(0,v[i]-c[i])
  print(sum(x))
if __name__ == '__main__':
  main()

Measured memory_bytes: 3064
N = int(input())
a1 = [int(x) for x in input().split()]
a2 = [int(x) for x in input().split()]

sum_over_1, sum_over_2 = 0, 0
a1_cum, a2_cum = [], []
tot_candies = []

for i in range(N) :
    if i == 0 :
        sum_over_1, sum_over_2 = a1[i], a2[N - i - 1]
        a1_cum.append(sum_over_1)
        a2_cum.append(sum_over_2)
        
    else :
        sum_over_1 += a1[i]
        sum_over_2 += a2[N - i - 1]
        
        a1_cum.append(sum_over_1)
        a2_cum.append(sum_over_2)

a2_cum_r = sorted(a2_cum, reverse = True)
for i in range(N) :
    tot_candies.append(a1_cum[i] + a2_cum_r[i])
    
print(max(tot_candies))

Measured memory_bytes: 3064
#include<iostream>
using namespace std;
int main()
{
	int s[10] = {0}, i, a, n,j;
	while (1) {
		cin >> n;
		if (n == 0)
			break;
		for (i = 0; i < n; i++) {
			cin >> a;
			s[a]++;
		}
		for (i = 0; i < 10; i++) {
			if (s[i] == 0)
				cout << "-";
			else {
				for (j = 0; j < s[i]; j++) {
					cout << "*";
				}
			}
			cout << endl;
			s[i] = 0;
		}
	}
	return 0;
}

Measured memory_bytes: 1152
void main() {
	auto N = ri;
	auto A = readAs!(int[]);
	auto m = A.reduce!max;
	auto l = A.reduce!min;
	writeln(m-l);
}

// ===================================

import std.stdio;
import std.string;
import std.conv;
import std.algorithm;
import std.range;
import std.traits;
import std.math;
import std.container;
import std.bigint;
import std.numeric;
import std.conv;
import std.typecons;
import std.uni;
import std.ascii;
import std.bitmanip;
import core.bitop;

T readAs(T)() if (isBasicType!T) {
	return readln.chomp.to!T;
}
T readAs(T)() if (isArray!T) {
	return readln.split.to!T;
}

T[][] readMatrix(T)(uint height, uint width) if (!isSomeChar!T) {
	auto res = new T[][](height, width);
	foreach(i; 0..height) {
		res[i] = readAs!(T[]);
	}
	return res;
}

T[][] readMatrix(T)(uint height, uint width) if (isSomeChar!T) {
	auto res = new T[][](height, width);
	foreach(i; 0..height) {
		auto s = rs;
		foreach(j; 0..width) res[i][j] = s[j].to!T;
	}
	return res;
}

int ri() {
	return readAs!int;
}

double rd() {
	return readAs!double;
}

string rs() {
	return readln.chomp;
}

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

int main()
{
  int64_t n;
  cin >> n;
  map<int64_t, int64_t> mp;

  for (int i = 0; i < n; i++) {
    int64_t a, b;
    cin >> a >> b;
    mp[b] += a;
  }

  string ans = "Yes";
  int64_t s = 0;
  for (auto&& e : mp) {
    s += e.second;
    if (s > e.first) {
      ans = "No";
      break;
    }
  }
  cout << ans << endl;
}


Measured memory_bytes: 12800
#include<bits/stdc++.h>
#define prArr(Arr,n) for(int _=0;_<n;_++)   cout<<(Arr)[_]<<" ";
using namespace std;
const int INF=(int)0x3f3f3f3f;
const int MOD=(int)1e9+7;
bool seen[101];
int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    int x,n,tmp,ans1,ans2;
    cin>>x>>n;
    for(int i=0;i<n&&cin>>tmp;i++)
        seen[tmp]=1;
    for(int i=x;i<200;i++)
        if(!seen[i]){ans1=i;break;}
    for(int i=x;i>=0;i--)
        if(!seen[i]){ans2=i;break;}
    cout<<(ans1-x<x-ans2?ans1:ans2);
    return 0;
}


Measured memory_bytes: 3652
r, c = map(int, input().split())
table = []
for i in range(r):
    table.append(list(map(int, input().split())))
    table[i].append(sum(table[i]))
last_row = []
for i in range(c + 1):
    tmp = 0
    for j in range(r):
        tmp += table[j][i]
    last_row.append(tmp)
table.append(last_row)

for i in range(r + 1):
    print(" ".join(list(map(str, table[i]))))


Measured memory_bytes: 5708
#include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>
#include <string>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <iomanip>
#include <utility>
#include <tuple>
#include <functional>
#include <bitset>
#include <cassert>
#include <complex>
#include <time.h>
#define ll long long
#define double long double
#define itn int
#define endl '\n'
#define co(ans) cout<<ans<<endl;
#define COYE cout<<"YES"<<endl;
#define COYe cout<<"Yes"<<endl;
#define COye cout<<"yes"<<endl;
#define CONO cout<<"NO"<<endl;
#define CONo cout<<"No"<<endl;
#define COno cout<<"no"<<endl;
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define FFOR(i,a,b) for(int i=(a);i<=(b);++i)
#define REP(i,n)  FOR(i,0,n)
#define RREP(i,n)  FFOR(i,1,n)
#define ALL(V) ((V).begin(),(V).end())
#define SORT(V) sort((V).begin(),(V).end())
#define REVERSE(V) reverse((V).begin(),(V).end())
#define INF ((1LL<<62)-(1LL<<31))
#define EPS 1e-10
#define PI 3.141592653589793238
#define MOD 1000000007
#define MAX 5100000
template<class T>bool chmax(T &a,const T &b){if(a<b){a=b;return 1;}return 0;}
template<class T>bool chmin(T &a,const T &b){if(b<a){a=b;return 1;}return 0;}
using namespace std;

int main(){
  double R;
  cin>>R;
  cout<<setprecision(4)<<2*R*PI<<endl;
}

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

int main()
{
    int n;
    cin >> n;
    int height[n];
    int temp, ans;

    ans = 0;

    for (int i = 0; i < n; i++) {
        cin >> height[i];
    }

    for (int i = 0; i < n; i++) {
        temp = 0;
        for (int k = 0; k < i; k++) {
            if (height[k] > temp) temp = height[k];
        }
        if (temp <= height[i]) ans += 1;
    }

    cout << ans << endl;

    return 0;
}


Measured memory_bytes: 256
loop do
  p, n = gets.split.map(&:to_i)
  break if n == 0
  p = Math.sqrt(p)
  up = (1..n).reduce([n, 1]) do |(x, y), i|
    j = (p * i).ceil
    break [x, y] if j > n
    (x.to_f / y - p) > (j.to_f / i - p) && j * y != i * x ? [j, i] : [x, y]
  end
  low = (1..n).reduce([1, n]) do |(x, y), i|
    j = (p * i).floor
    break [x, y] if j > n
    (p - x.to_f / y) > (p - j.to_f / i) && j * y != i * x ? [j, i] : [x, y]
  end
  puts "#{up[0]}/#{up[1]} #{low[0]}/#{low[1]}"
end

Measured memory_bytes: 8744
import sys

if sys.platform =='ios':
    sys.stdin=open('input_file.txt')
   
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

k=int(input())
S=input()

if len(S)>k:
	print(S[:k]+"...")
else:
	print(S)

Measured memory_bytes: 9180
import java.util.Scanner;

public class Main{
  public static void main(String[] args){

   Scanner sc = new Scanner(System.in);

   int x = sc.nextInt();
   int y = sc.nextInt();

   int[] array = {0,2,0,1,0,1,0,0,1,0,1,0};

   if(array[x - 1] == array[y - 1]){
     System.out.println("Yes");
   }else{
    System.out.println("No");
   }
  }
}

Measured memory_bytes: 21844
#include "bits/stdc++.h"
using namespace std;
typedef long long ll;

class KMP {
public:
    string pattern;
    int plen;
    vector<int> table;

    // tからsを探す(探すものを引数に)
    KMP(const string& s) : pattern(s), plen((int)pattern.size()), table(plen + 1) {
        table[0] = -1;
        int j = -1;
        for (int i = 0; i < plen; i++) {
            while (j >= 0 && pattern[i] != pattern[j]) j = table[j];
          	j++;
            if (i + 1 < plen && pattern[i + 1] == pattern[j]) table[i + 1] = table[j];
            else table[i + 1] = j;
        }
    }

    // マッチする左端の箇所
    vector<int> search(const string& text) {
        vector<int> res;
        int head = 0, j = 0, tlen = (int)text.size();
        while (head + j < tlen) {
            if (pattern[j] == text[head + j]) {
                if (++j != plen) continue;
                res.push_back(head);
            }
            head += j - table[j], j = max(table[j], 0);
        }
        return res;
    }
};

struct DAG {
private:
    struct Edge {
        int to;
    };
    std::vector<std::vector<Edge>> graph;
    bool is_dag = false;
    std::vector<int> sorted; // トポロジカルソート
    int V; // 頂点数
public:

    DAG(int v) {
        assert(v > 0);
        V = v;
        graph.resize(v);
    }

    // from から to への有向辺を張る
    void add_edge(int from, int to) {
        graph[from].push_back({to});
    }

    // トポロジカルソート O(V + E)
    // DAG じゃないなら size 0 の vectorを返す
    std::vector<int> topological_sort(int &ret) {
        ret = 0;
        std::stack<int> sta;
        std::vector<int> dist(V, 0);//その頂点までの最長路
        std::vector<int> in(V, 0);// 入次数
        int used_cnt = 0;//使用した頂点の数
        for (int i = 0; i < V; i++) {
            for (Edge e : graph[i]) {
                in[e.to]++;
            }
        }
        for (int i = 0; i < V; i++) if (in[i] == 0) {
            sta.push(i);
            used_cnt++;
        }
        while (!sta.empty()) {
            int p = sta.top(); sta.pop();
            sorted.push_back(p);
            for (Edge e : graph[p]) {
                int v = e.to;
                in[v]--;
                dist[v] = std::max(dist[v], dist[p] + 1);
                ret = max(ret, dist[v]);
                if (in[v] == 0) {
                    sta.push(v);
                    used_cnt++;
                }
            }
        }
        if (used_cnt == V) {
            return sorted;
        }
        else {
            return std::vector<int>(0);
        }
    }

    vector<Edge>& operator[](int x) {
        return graph[x];
    }
};

int main() {
    string s, t; cin >> s >> t;
    string s2 = s;
    int n = s.size();
    DAG g(n);
    while (n + t.size() - 1 > s.size()) {
        s += s2;
    }
    KMP kmp(t);
    //cout << "s : " << s << endl;
    for (int v : kmp.search(s)) {
        if (v >= n) break;
        g.add_edge(v, (v + t.size()) % n);
        //cout << v << " " << (v + t.size()) % n << endl;
    }
    int ans;
    if (g.topological_sort(ans).size() == 0) {
        cout << -1 << endl;
    }
    else {
        //cout << g.topological_sort(ans).size() << endl;;
        cout << ans << endl;
    }
    return 0;
}

Measured memory_bytes: 36056
#include <bits/stdc++.h>
using namespace std;
#define FOR(i,k,n) for(int i = (k); i < (n); i++)
#define REP(i,n) FOR(i,0,n)
#define INF 114514810
#define ALL(a) (a).begin(),(a).end()
#define pb(x) push_back(x)
#define ms(m,v) memset(m,v,sizeof(m))
#define D10  fixed<<setprecision(10) 
typedef vector<int> vi;
typedef vector<string> vs;
typedef pair<int, int> P;
typedef long long ll;
struct edge{ int from, to, cost; };

struct data{ int id, ac, pe; };
bool comp(const data& d1, const data& d2)
{
	if (d1.ac != d2.ac) return d1.ac > d2.ac;
	else if (d1.pe != d2.pe) return d1.pe < d2.pe;
	else return d1.id < d2.id;
}

int main()
{
	int t, p, r;
	while (cin >> t >> p >> r, t)
	{
		int wa[55][15];
		ms(wa, 0);
		data d[55];
		FOR(i, 1, t + 1) d[i] = data{ i, 0, 0 };
		REP(w, r)
		{
			int tid, pid, time;
			string mes;
			cin >> tid >> pid >> time >> mes;
			if (mes == "WRONG") wa[tid][pid] += 1200;
			else
			{
				d[tid].ac++;
				d[tid].pe += time + wa[tid][pid];
			}
		}
		sort(d + 1, d + t + 1, comp);
		FOR(i, 1, t + 1) cout << d[i].id << " " << d[i].ac << " " << d[i].pe << endl;
	}
	return 0;
}

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

int main(){
  
  long N,K;
  cin >>N >>K;
  
  long Q;
  Q=N/K;
  long hi,low;
  hi=abs(N-Q*K);
  low=abs(N-(Q+1)*K);
  if(hi<abs(low)){
    cout <<hi <<endl;
  }else{
    cout <<low <<endl;
  }
  
}


Measured memory_bytes: 256
#include<iostream>
#include<stdio.h>
#include<vector>
#include<algorithm>
#include<set>
#include<string>
#include<map>
#include<string.h>
#include<complex>
#include<math.h>
#include<queue>
#include <functional>
#include<time.h>
#include <stack>
#include<iomanip>
#include<unordered_map>
using namespace std;
#define rep(i,a,n) for(int i=(a);i<(n);i++)
#define ll long long
#define llint long long int
#define sort(s) sort(s.begin(),s.end())
#define reverse(v)  reverse(v.begin(), v.end());
#define Yes(ans) if(ans)cout<<"Yes"<<endl; else cout<<"No"<<endl;
#define YES(ans) if(ans)cout<<"YES"<<endl; else cout<<"NO"<<endl;
#define hei(a) vector<a>
#define whei(a) vector<vector<a>>
#define UF  UnionFind
#define rt (a); return (a); 
#define Pint pair<int,int>
#define Pll pair<ll,ll>
#define keta(a)  fixed << setprecision(a)
constexpr auto INF = 1000000000;
constexpr auto mod = 1000000007;
ll gcd(ll a, ll b) {
	if (a < b)swap(a, b);
	if (b == 0)return a;
	return gcd(b, a % b);
}

struct UnionFind {
	vector<int> par; // par[i]:iの親の番号　(例) par[3] = 2 : 3の親が2

	UnionFind(int N) : par(N) { //最初は全てが根であるとして初期化
		for (int i = 0; i < N; i++) par[i] = i;
	}
	int size(int a) {
		return par[root(a)];
	}

	int root(int x) { // データxが属する木の根を再帰で得る：root(x) = {xの木の根}
		if (par[x] == x) return x;
		return par[x] = root(par[x]);
	}

	void unite(int x, int y) { // xとyの木を併合
		int rx = root(x); //xの根をrx
		int ry = root(y); //yの根をry
		if (rx == ry) return; //xとyの根が同じ(=同じ木にある)時はそのまま
		par[rx] = ry; //xとyの根が同じでない(=同じ木にない)時：xの根rxをyの根ryにつける
	}

	bool same(int x, int y) { // 2つのデータx, yが属する木が同じならtrueを返す
		int rx = root(x);
		int ry = root(y);
		return rx == ry;
	}
};
int gcd(int a, int b) {
	if (b > a)swap(b, a);
	if (b == 0)return a;
	return gcd(b, a % b);
}

int main() {
	int x;
	cin >> x;
	int ans = 360  / gcd(360, x);
	cout << ans << endl;
	return 0;
}

Measured memory_bytes: 3620
#include<bits/stdc++.h>
using namespace std;
int main(){
  int n;
  cin>>n;
  vector<int> h(n+5);
  for(int i=0;i<n;i++){
    cin>>h[i+1];
  }
  vector<int> dp(n+5,100000);
  dp[1]=0;
  dp[2]=abs(h[1]-h[2]);
  for(int i=3;i<=n;i++){
    dp[i]=min(dp[i-2]+abs(h[i]-h[i-2]),dp[i-1]+abs(h[i]-h[i-1]));
  }
  cout<<dp[n]<<'\n';
  return(0);
}

Measured memory_bytes: 4004
def solve() :
    ri = raw_input()

    inarr = ri.split(' ')
    h = inarr[0]
    w = inarr[1]

    print '#' * (int(w) + 2)
    for i in range(0, int(h)) :
        line = raw_input()
        print ('#' + line + '#')
    print '#' * (int(w) + 2)

if __name__ == '__main__':
    solve()

Measured memory_bytes: 2568