akhauriyash/Code-Regression huggingface dataset:
#include "bits/stdc++.h"
using namespace std;

int main() {
        int n;
        string s;
        cin >> n >> s;
        int ans = 0;
        int x = 0;
        for (int i = 0; i < s.size(); i ++) {
                if (s[i] == 'I') x ++;
                else x --;
                ans = max(ans, x);
        }
        cout << ans << endl;
        return 0;
}


Measured memory_bytes: 256
N,A,B=map(int,input().split())
count=0
for i in range (1,N+1):
    data='%05d'%(i)
    data_add=int(data[0])+int(data[1])+int(data[2])+int(data[3])+int(data[4])
    if data_add>=A and data_add<=B:
        count+=i
print(count)

Measured memory_bytes: 3064
bingo = [None]*3
for i in range(3):
    bingo[i] = list(map(int,input().split()))
N= int(input())
lis = [None]*N
for i in range(N):
    temp = int(input())
    for j in range(3):
        for k in range(3):
            if bingo[j][k] == temp:
                bingo[j][k] = 0
                break

if bingo[0][0] == bingo[1][1] == bingo[2][2] == 0:
    print('Yes')
elif bingo[0][2] == bingo[1][1] == bingo[2][0] == 0:
    print('Yes')

else:
    ans ="No"
    for i in range(3):
        if bingo[0][i] == bingo[1][i] == bingo[2][i] == 0 or bingo[i][0]==bingo[i][1]==bingo[i][2]==0:
            ans = 'Yes'
    print(ans)


Measured memory_bytes: 3064
#include <iostream>
#include <utility>
#include <map>
#include <vector>
#include <algorithm>
#include <queue>
#include <math.h>
#include <stack>
#include <set>
#include <deque>
#include <cmath>

using namespace std;
typedef long long ll;
ll mod = 1e9+7;
//ll mod = 998244353;
#define rep(i,n) for(int i = 0; i < (n); ++i)

int main() {
    int k, s;
    cin >> k >> s;
    int ans = 0;
    rep(i, k+1) rep(j, k+1) {
        if (i + j > s) continue;
        else if (s - (i + j) <= k) {
            ++ans;
        }
    }
    cout << ans << endl;
    return 0;
}


Measured memory_bytes: 3532
#include<stdio.h>
#define N 1000001
char prime_list[N];
char dp[N];
int dish_price[30];
void furui(void) {
  int i, j;
  for (i = 0; i < N; i ++) prime_list[i] = 1;
  prime_list[0] = 0; prime_list[1] = 0;
  for (i = 2; i * i < N; i ++) for (j = i * i; j < N; j += i) prime_list[j] = 0;
}
void solve(int n){
  int i, j;
  for (i = 0; i < N; i ++) dp[i] = 0;
  dp[0] = 1;
  for (i = 0; i < N; i ++) for (j = 0; j < n; j ++) if (i >= dish_price[j]) dp[i] |= dp[i - dish_price[j]];
}
int main() {
  int n, x, i;
  furui();
  while (1) {
    scanf("%d %d", &n, &x);
    if (!(n||x)) break;
    for (i = 0; i < n; i ++) scanf("%d", &dish_price[i]);
    solve(n);
    for (i = x; i > 0; i --){
      if (dp[i] == 1 && prime_list[i] == 1) {
	printf("%d\n", i); break;
      }
    }
    if (!i) printf("NA\n");
  }
  return 0;
}

Measured memory_bytes: 2564
#
# 　　  ⋀_⋀　 
#　　  (･ω･)  
# .／ Ｕ ∽ Ｕ＼
#  │＊　合　＊│
#  │＊　格　＊│ 
#  │＊　祈　＊│ 
#  │＊　願　＊│ 
#  │＊　　　＊│ 
#      ￣
#
import sys
input=sys.stdin.readline
inf=float('inf')
mod = 10**9+7
def INT_(n): return int(n)-1
def MI(): return map(int,input().split())
def MF(): return map(float, input().split())
def MI_(): return map(INT_,input().split())
def LI(): return list(MI())
def LI_(): return [int(x) - 1 for x in input().split()]
def LF(): return list(MF())
def LIN(n:int): return [input() for _ in range(n)]
def LLIN(n: int): return [LI() for _ in range(n)]
def LLIN_(n: int): return [LI_() for _ in range(n)]
def LLI(): return [list(map(int, l.split() )) for l in input()]
def I(): return int(input())
def F(): return float(input())
def ST(): return input().replace('\n', '')
def main():
    from math import ceil
    a, b = MI()
    print(ceil((a+b)/2))
if __name__ == '__main__':
    main()

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

int main() {
  ios::sync_with_stdio(0); cin.tie(0);
  int n, m;
  cin >> n >> m;
  int a = (n-(n%2==0)+1)/4, b = (n-(n%2==0)-1)/4;
  vector<pair<int,int>> ans;
  for (int i = 0; i < a; i++) {
    ans.emplace_back(a-i,a+1+i);
  }
  for (int i = 0; i < b; i++) {
    ans.emplace_back(a*2+b-i,a*2+b+2+i);
  }
  ans.resize(m);
  for (auto [x,y] : ans) cout << x << ' ' << y << endl;
}


Measured memory_bytes: 4196
#ifndef LOCAL
#pragma GCC optimize("O3")
#endif
#include "bits/stdc++.h"
using namespace std;
#define sim template < class c
#define ris return * this
#define dor > debug & operator <<
#define eni(x) sim > typename \
  enable_if<sizeof dud<c>(0) x 1, debug&>::type operator<<(c i) {
sim > struct rge { c b, e; };
sim > rge<c> range(c i, c j) { return rge<c>{i, j}; }
sim > auto dud(c* x) -> decltype(cerr << *x, 0);
sim > char dud(...);
struct debug {
#ifdef LOCAL
~debug() { cerr << endl; }
eni(!=) cerr << boolalpha << i; ris; }
eni(==) ris << range(begin(i), end(i)); }
sim, class b dor(pair < b, c > d) {
  ris << "(" << d.first << ", " << d.second << ")";
}
sim dor(rge<c> d) {
  *this << "[";
  for (auto it = d.b; it != d.e; ++it)
    *this << ", " + 2 * (it == d.b) << *it;
  ris << "]";
}
#else
sim dor(const c&) { ris; }
#endif
};
#define imie(...) " [" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "] "
typedef long long ll;
const ll MAXW = 1e5 + 10 ;
ll n , W ;
ll w[110] , v[110] ;
ll dp[110][MAXW] ;
ll dfs(ll x,ll currWeight){
    if ( x == n ) {
        return 0 ;
    }
    if ( dp[x][currWeight] ) return dp[x][currWeight] ;
    ll choice1 = 0 ;
    ll choice2 = 0 ;
    if ( currWeight < w[x] ){
        choice1 = dfs(x+1,currWeight) ;
    }
    else{
        choice1 = dfs(x+1,currWeight) ;
        choice2 = v[x] + dfs(x+1,currWeight-w[x]) ;
    }
    dp[x][currWeight] = max(choice1,choice2) ;
    return dp[x][currWeight] ;
}

int main(){
    cin >> n >> W ;
    for(int i=0;i<n;i++){
        cin >> w[i] >> v[i] ;
    }
    cout << dfs(0,W) << endl;
    return 0 ;
}


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

using namespace std;

int main() {
	int A, B;
	double D, E;

	cin >> A >> B;

	D = A;
	E = B;

	double a = D / E;

	cout << A / B << " " << A % B << " " << endl;
	std::cout << std::fixed;
	std::cout << std::setprecision(5) << a << endl;

	return 0;
}


Measured memory_bytes: 3284
#include<iostream>
#include<string>
#include<algorithm>
#include<bits/stdc++.h>
#include<iomanip>
using namespace std;

int MOD=1000000007;

int main()
{
  int N,F[100][100]={},P[100][100]={},ans=-MOD;
  cin >> N;
for(int i=0;i<N;i++) { 
  for(int j=0;j<10;j++) cin >> F[i][j]; 
  } 
  for(int i=0;i<N;i++){
  for(int j=0;j<11;j++) cin >> P[i][j];
  }
  int res=-(1<<30); 
  for(int i = 1; i < (1 << 10); i++){
    int c = 0;
    for(int j = 0; j < N; j++){
      int cnt = 0;
      for(int k = 0; k < 10; k++){
	if((i & (1 << k)) && F[j][k])cnt++;
      }
      c += P[j][cnt]; 
    }
    ans = max(ans, c);
  }
  
  cout << ans <<endl;
  return 0;
}


Measured memory_bytes: 384
/*
    Author: Nguyen Tan Bao
    Status:
    Idea:
*/

#include <bits/stdc++.h>
#define FI first
#define SE second
#define EPS 1e-9
#define ALL(a) a.begin(),a.end()
#define SZ(a) int((a).size())
#define MS(s, n) memset(s, n, sizeof(s))
#define FOR(i,a,b) for (int i = (a); i <= (b); i++)
#define FORE(i,a,b) for (int i = (a); i >= (b); i--)
#define FORALL(it, a) for (__typeof((a).begin()) it = (a).begin(); it != (a).end(); it++)
//__builtin_ffs(x) return 1 + index of least significant 1-bit of x
//__builtin_clz(x) return number of leading zeros of x
//__builtin_ctz(x) return number of trailing zeros of x

using namespace std;
using ll = long long;
using ld = double;
typedef pair<int, int> II;
typedef pair<II, int> III;
typedef complex<ld> cd;
typedef vector<cd> vcd;

const ll MODBASE = 1000000007LL;
const int MAXN = 210;
const int MAXM = 20;
const int MAXK = 16;
const int MAXQ = 200010;

ll MOD = MODBASE;
ll f[MAXN], rev[MAXN];

ll binPowMod(ll a, ll b) {
    a %= MOD;
    ll res = 1;
    while (b > 0) {
        if (b & 1LL) res = res * a % MOD;
        a = a * a % MOD;
        b >>= 1LL;
    }
    return res;
}

ll add(ll a, ll b) {
    return (a + b) % MOD;
}

ll sub(ll a, ll b) {
    return (a - b + MOD) % MOD;
}

ll mul(ll a, ll b) {
    return a * b % MOD;
}

ll C(int n, int k) {
    if (k > n || k < 0) return 0;
    return mul(f[n], mul(rev[n-k], rev[k]));
}

ll calc(int n) {
    ll res = 0;
    for (int i = 0; 2 * i + 5 <= n; i++) res = add(res, mul(C(i+4, 4), C(n+5-2*i, 10)));
    return res;
}

ll inv(ll x) {
    return binPowMod(x, MOD-2);
}

ll LagrangeInterpolation(vector<ll> &y, int n) {
    if (n <= 15) return y[n];
    ll numerator = 1, denominator = 1;
    FOR(i,0,15) {
        numerator = mul(numerator, n - i);
        ll x = (0 - i + MOD) % MOD;
        if (i > 0) denominator = mul(denominator, x);
    }
    ll res = 0;
    FOR(i,0,15) {
        res = add(res, mul(y[i], mul(mul(numerator, inv(n - i)), inv(denominator))));
        ll last = (i - 15 + MOD) % MOD;
        denominator = mul(denominator, inv(last));
        ll newFirst = (i+1) - 0;
        denominator = mul(denominator, newFirst);
    }
    return res;
}

ll solve(int n) {
    if (n < 5) return 0;
    vector<ll> y(16);
    int r = (n - 5) % 2, N = (n - 5) / 2;
    FOR(i,0,15) y[i] = calc(2*i + 5 + r);
    return LagrangeInterpolation(y, N);
}

void init() {
    f[0] = 1;
    rev[0] = inv(f[0]);
    FOR(i,1,200) {
        f[i] = mul(f[i-1], i);
        rev[i] = inv(f[i]);
    }
}

int main() {
    ios::sync_with_stdio(0);
    cin.tie(nullptr);
    init();
    int t;
    cin >> t;
    while (t--) {
        int n;
        cin >> n;
        cout << solve(n) << "\n";
    }
    return 0;
}


Measured memory_bytes: 3664
#include <iostream>
#include <string>
#include <vector>
#include <queue>

using namespace std;

#define loop(i,a,b) for(int i=(a); i<(int)(b); i++)
#define rep(i,b) loop(i,0,b)

struct edge {
    int to, cost;
};

struct state {
    int v, d, rest;
    bool operator<(const state& s)const{
        return d>s.d;
    }
};

int dijk(int s, int g, const vector<vector<edge>>& G, int c){

    vector<vector<int>> d(G.size(), vector<int>(c+1,1000000000));
    d[s][0] = 0;

    priority_queue<state> q;
    q.push({ s, 0, c });

    //vector<vector<bool>> vis(G.size(), vector<bool>(c + 1, false));

    while (q.size()){
        state sta = q.top();
        q.pop();

        int v = sta.v;

        int rest = sta.rest;
/*
        if (vis[v][rest]) continue;
        vis[v][rest] = true;*/

        rep(i, G[v].size()){

            int to = G[v][i].to;
            int cost = G[v][i].cost;

            if (d[to][rest] > sta.d + cost){

                d[to][rest] = sta.d + cost;
                q.push({ to, sta.d + cost, rest });
            }

            if (d[to][rest] > sta.d + cost / 2 && rest != 0){

                d[to][rest] = sta.d + cost / 2;
                q.push({ to, sta.d + cost / 2, rest - 1 });
            }
        }
    }
    int ans = 1 << 30;
    rep(j, d[0].size())if (d[g][j] != 1000000000){
        ans = min(ans, d[g][j]);
    }
    return ans;
}

int main(){
    while (1){
        int c, V, E, s, d;
        cin >> c >> V >> E >> s >> d;
        if (c == 0) break;
        vector<vector<edge>> G(V);
        s--; d--;
        rep(i, E){
            int a, b, c;
            cin >> a >> b >> c;
            a--; b--;
            G[a].push_back({ b, c });
            G[b].push_back({ a, c });
        }
        cout << dijk(s, d, G, c) << endl;
    }
}

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

#define FOR(i,l,r) for(int i = (l);i < (r);i++)
#define ALL(x) (x).begin(),(x).end()
template<typename T> void chmax(T& a,const T& b){if(a < b) a = b;}
template<typename T> void chmin(T& a,const T& b){if(b < a) a = b;}
typedef long long ll;

int N;
const ll INF = 2e18;

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

	cin >> N;
	vector<ll> T(N),A(N);
	FOR(i,0,N){
		cin >> T [i] >> A [i];
	}
	FOR(i,1,N){
		ll x = max((T [i - 1] + T [i] - 1) / T [i],(A [i - 1] + A [i] - 1) / A [i]);
		T [i] *= x,A [i] *= x;
	}

	cout << T [N - 1] + A [N - 1] << endl;

	return 0;
}


Measured memory_bytes: 256
from sys import stdin


if __name__ == "__main__":
    _in = [_.rstrip() for _ in stdin.readlines()]
    n = int(_in[0])  # type:int
    a_arr = list(map(int,_in[1].split(' ')))  # type:list(int)
    # vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
    cnt0 = 0
    sum_ = 0
    for i in range(0,n):
        if i%2 == 0:
            if sum_+a_arr[i]<=0:
                cnt0 += 1-(sum_+a_arr[i])
                sum_ = 1
            else:
                sum_ = sum_+a_arr[i]
        else:
            if sum_+a_arr[i]>=0:
                cnt0 += 1+(sum_+a_arr[i])
                sum_ = -1
            else:
                sum_ = sum_+a_arr[i]
    cnt1 = 0
    sum_ = 0
    for i in range(0,n):
        if i%2 == 1:
            if sum_+a_arr[i]<=0:
                cnt1 += 1-(sum_+a_arr[i])
                sum_ = 1
            else:
                sum_ = sum_+a_arr[i]
        else:
            if sum_+a_arr[i]>=0:
                cnt1 += 1+(sum_+a_arr[i])
                sum_ = -1
            else:
                sum_ = sum_+a_arr[i]
    cnt = min(cnt0,cnt1)
    # ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    print(cnt)


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

using namespace std;

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

#define FOR(i, n, m) for(ll (i)=(m);(i)<(n);++(i))
#define REP(i, n) FOR(i,n,0)
#define OF64 std::setprecision(10)

const ll MOD = 1000000007;
const ll INF = (ll) 1e15;

class Prime {
public:
    Prime(ll n) {
        mPrimeTbl.clear();
        vector<bool> u(n, false);
        for (ll i = 2; i <= n; ++i) {
            if (u[i])
                continue;
            mPrimeTbl.push_back(i);
            for (ll p = i; p <= n; p += i)
                u[p] = true;
        }
    }

    ~Prime() {}

    ll num() {
        return static_cast<ll>(mPrimeTbl.size());
    }

    ll get(ll index) {
        return mPrimeTbl[index];
    }

private:
    vector<ll> mPrimeTbl;
};

int main() {
    cin.tie(0);
    Prime prime(1000005);
    ios::sync_with_stdio(false);
    ll N;
    cin >> N;

    vector<pll> p;
    REP(i, prime.num()) {
        ll t = prime.get(i);
        ll n = 0;
        while (N % t == 0) {
            n++;
            N /= t;
        }
        if (n > 0)
            p.push_back(pll(t, n));
    }

    if (N > 1)
        p.push_back(pll(N, 1));

    ll ans = 0;
    REP(i, p.size()) {
        //cout << p[i].first << " " << p[i].second << endl;
        ll n = 0;
        ll m = 1;
        while (n + m <= p[i].second) {
            n += m;
            m++;
        }
        ans += m - 1;
    }
    cout << ans << endl;
    return 0;
}

Measured memory_bytes: 4252
n,k,i,y;d['   '];c(int*z){y=*1[&z]-*z;}main(x){for(scanf("%*d");~scanf("%d%d",&n,&k);printf("%d\n",i)){for(i=n--;i;y=x)scanf("%d",&x),d[--i]=x-y;for(qsort(d,n,4,c);n>=k;i+=d[--n]);}}

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


int main(){
    int k, a, b;
    cin >> k;
    cin >> a >> b;

    int largest = (b/k) *k;
    if(a <= largest){
        cout << "OK" << endl;
    } else {
        cout << "NG" << endl;
    }
    return 0;
}

Measured memory_bytes: 3660
import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		int k = sc.nextInt();
		
		double[] cs = new double[n];
		cs[0] = (1 + sc.nextInt()) / 2.0;
		
		for (int i = 1; i < n; i++) {
			int p = sc.nextInt();
			cs[i] = cs[i - 1] + (1 + p) / 2.0;
		}
		
		double max = cs[k - 1];
		for (int i = k; i < n; i++) {
			max = Math.max(max, cs[i] - cs[i - k]);
		}
		
		System.out.println(max);
		
		sc.close();
	}

}




Measured memory_bytes: 51916
using System;
using System.Linq;
using System.Collections.Generic;
using static System.Console;
using System.Text;
using System.IO;
using static System.Math;
namespace AtCoder
{
    class Program
    {
        static public long[] Sarray() { return ReadLine().Split().Select(long.Parse).ToArray(); }
        static public List<long> Slist() { return ReadLine().Split().Select(long.Parse).ToList(); }

        static readonly long Mod = 1000000000 + 7;

        static void Main(string[] args)
        {
            //SetOut(new StreamWriter(OpenStandardOutput()) { AutoFlush = false });
            var HW = Sarray();
            var H = HW[0];
            var W = HW[1];
            var masu = new List<string>();
            masu.Add(new string('.', (int)W + 2));
            for (var i = 0; i < H; ++i)
                masu.Add("." + ReadLine() + ".");
            masu.Add(new string('.', (int)W + 2));
            for (var i = 1; i < H + 1; ++i)
            {
                for (var j = 1; j < W + 1; ++j)
                {
                    if (masu[i][j] == '#')
                        Write('#');
                    else
                    {
                        var cnt = 0;
                        for (var k = -1; k <= 1; ++k)
                            for (var l = -1; l <= 1; ++l)
                                if (masu[i - k][j - l] == '#')
                                    ++cnt;
                        Write(cnt);

                    }
                }
                WriteLine();
            }
            //Out.Flush();
        }
    }
}

Measured memory_bytes: 13396
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<vector>
#include<map>
#include<queue>
#include<algorithm>
#include<functional>

using namespace std;

using ll=long long;
using vi=vector<int>;
using vvi=vector<vi>;
using pii=pair<int,int>;
using vll=vector<ll>;
using vvll=vector<vll>;


void min_u(ll&m, ll v)
{
	if(m>v) m=v;
}


ll solve(vll&s, vll&t, ll x)
{
	int sn=s.size(), tn=t.size();
	int si=lower_bound(s.begin(), s.end(), x)-s.begin();
	int ti=lower_bound(t.begin(), t.end(), x)-t.begin();
	ll ret=1000000000000;

	// sptp
	if(si<sn && ti<tn) min_u(ret, max(s[si], t[ti])-x);
	// sptn
	if(si<sn && ti>0) min_u(ret, s[si]-x+(s[si]-t[ti-1]));
	// sntp
	if(si>0 && ti<tn) min_u(ret, x-s[si-1]+(t[ti]-s[si-1]));
	// tpsn
	if(si>0 && ti<tn) min_u(ret, t[ti]-x+(t[ti]-s[si-1]));
	// tnsp
	if(si<sn && ti>0) min_u(ret, x-t[ti-1]+(s[si]-t[ti-1]));
	// sntn
	if(si>0 && ti>0) min_u(ret, x-min(s[si-1], t[ti-1]));
	return ret;
}


int main(void)
{
	int a, b, q;

	while(scanf("%d%d%d", &a, &b, &q)==3)
	{
		vll s(a), t(b);

		for(int i=0;i<a;i++)
		{
			scanf("%lld", &s[i]);
		}
		for(int i=0;i<b;i++)
		{
			scanf("%lld", &t[i]);
		}
		for(int i=0;i<q;i++)
		{
			ll x;
			scanf("%lld", &x);
			printf("%lld\n", solve(s, t, x));
		}
	}

	return 0;
}


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

typedef long long   signed int LL;
typedef long long unsigned int LU;

#define incID(i, l, r) for(int i = (l)    ; i <  (r); i++)
#define incII(i, l, r) for(int i = (l)    ; i <= (r); i++)
#define decID(i, l, r) for(int i = (r) - 1; i >= (l); i--)
#define decII(i, l, r) for(int i = (r)    ; i >= (l); i--)
#define  inc(i, n) incID(i, 0, n)
#define inc1(i, n) incII(i, 1, n)
#define  dec(i, n) decID(i, 0, n)
#define dec1(i, n) decII(i, 1, n)

#define inII(v, l, r) ((l) <= (v) && (v) <= (r))
#define inID(v, l, r) ((l) <= (v) && (v) <  (r))

#define PB push_back
#define EB emplace_back
#define MP make_pair
#define FI first
#define SE second
#define PQ priority_queue

#define  ALL(v)  v.begin(),  v.end()
#define RALL(v) v.rbegin(), v.rend()
#define  FOR(it, v) for(auto it =  v.begin(); it !=  v.end(); ++it)
#define RFOR(it, v) for(auto it = v.rbegin(); it != v.rend(); ++it)

template<typename T> bool   setmin(T & a, T b) { if(b <  a) { a = b; return true; } else { return false; } }
template<typename T> bool   setmax(T & a, T b) { if(b >  a) { a = b; return true; } else { return false; } }
template<typename T> bool setmineq(T & a, T b) { if(b <= a) { a = b; return true; } else { return false; } }
template<typename T> bool setmaxeq(T & a, T b) { if(b >= a) { a = b; return true; } else { return false; } }
template<typename T> T gcd(T a, T b) { return (b == 0 ? a : gcd(b, a % b)); }
template<typename T> T lcm(T a, T b) { return a / gcd(a, b) * b; }

// ---- ----

int h, w, d, a[300][300], q;
pair<int, int> pos[300 * 300];
int sum[300 * 300];

int main() {
	cin >> h >> w >> d;
	inc(i, h) {
	inc(j, w) {
		cin >> a[i][j];
		a[i][j]--;
		pos[a[i][j]] = MP(i, j);
	}
	}
	
	incID(i, d, h * w) {
		sum[i] = sum[i - d] + abs(pos[i].FI - pos[i - d].FI) + abs(pos[i].SE - pos[i - d].SE);
	}
	
	cin >> q;
	inc(qq, q) {
		int l, r;
		cin >> l >> r;
		l--; r--;
		
		cout << sum[r] - sum[l] << "\n";
	}
	
	return 0;
}


Measured memory_bytes: 2560
// IOI 2021
 
#include <bits/stdc++.h>

using namespace std;

#define endl '\n'
#define ends ' '
#define die(x) return cout << x << endl, 0
#define all(v) v.begin(), v.end()
#define sz(x) (int)(x.size())
#define debug(x) cerr << #x << ": " << x << endl
#define debugP(p) cerr << #p << ": {" << p.first << ", " << p.second << '}' << endl
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
const ll INF = 1e9;
const ll MOD = 1e9 + 7;

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

const int N = 5e3 + 5;

int A[N];

int main() {

	ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);

	ll sum = 0;
	int n, k, ans = 0; cin >> n >> k;
	for (int i = 0; i < n; i++) cin >> A[i];
	sort(A, A + n, greater<int>());
	for (int i = 0; i < n; i++) {
		sum += A[i];
		if (sum < k) ans++;
		else sum -= A[i], ans = 0;
	}
	cout << ans << endl;

	return 0;
}


Measured memory_bytes: 256
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <vector>
#include <list>
#include <algorithm>
#include <functional>
#include <map>
#include <set>
#include <cstring>
#include <string>
#include <cctype>
#include <cassert>
#include <queue>
#include <unordered_set>
#include <unordered_map>

using namespace std;

#define pb push_back
#define mp make_pair
#define all(x) (x).begin(), (x).end()
#define rep(i,n) for(int i = 0; i < (n); i++)
#define repr(i,b,e) for(int i = (b); i <= (e); i++)
#define INF (1001001001)
#define EPS (1e-15)

#define pr(x) do{cerr << (#x) << " = " << (x) << endl;}while(0)
#define pri(x,i) do{cerr << (#x) << "[" << i << "] = " << (x[i]) << endl;}while(0)
#define pra(x) do{rep(__i,(x).size()) pri(x,__i);}while(0)
#define pran(x,n) do{rep(__i,n) pri(x,__i);}while(0)
#define pral(x) do{cerr << (#x) << " =";rep(__i,(x).size()) cerr<<" "<<(x[__i]);cerr<<endl;}while(0)
#define prar(x,b,e) do{repr(__i,b,e) pri(x,__i);}while(0)

typedef long long ll;
typedef pair<int, int> pint;
typedef vector<int> vint;
typedef vector<pint> vpint;

template<class T> ostream& operator<<(ostream &os, const vector<T> &t) {os<<"{"; rep(i,t.size()) {os<<t[i]<<",";} os<<"}"<<endl; return os;}
template<class T,size_t n> ostream& operator<<(ostream &os, const array<T,n> &t) {os<<"{"; rep(i,n) {os<<t[i]<<",";} os<<"}"<<endl; return os;}
template<class S, class T> ostream& operator<<(ostream &os, const pair<S,T> &t) { return os<<"("<<t.first<<","<<t.second<<")";}
template<class T> inline bool CHMAX(T &l,const T &r){return l<r?l=r,1:0;}
template<class T> inline bool CHMIN(T &l,const T &r){return l>r?l=r,1:0;}

ll MOD = 1000000007;

function<void(int,int)> tensi_no_3p=[&](int v,int d){
};

int in() {
	int a;
	scanf("%d ", &a);
	return a;
}

int main() {
	int N; cin >> N;
	string S; cin >> S;
	int bc = 0, ac = 0;
	int ans = INF;
	rep(i, N) if (S[i] == 'E') bc++;
	rep(i,N) {
		if (S[i] == 'E') bc--;
		CHMIN(ans, bc + ac);
		if (S[i] == 'W') ac++;
	}
	cout << ans << endl;
	return 0;
}


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

int main() {
  int N;
  string c;
  cin >> N;
  for(int i = 0;i < N;i++){
    cin >> c;
    if(c == "Y"){
      cout << "Four" <<endl;
      N = 0;
      break;
    }
  }
  if(N){
    cout << "Three" << endl;
  }
}

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

typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef long long ll;


#define SZ(x) ((int)(x).size())
#define BIT(n) (1ll<<(n))
#define SORT(c) sort((c).begin(),(c).end())
#define RALL(a) (a).rbegin(), (a).rend()
#define ALL(x) (x).begin(),(x).end()
#define PB push_back
#define MP make_pair
#define CLR(a) memset((a), 0 ,sizeof(a))
#define retrun return
#define INF 1000000010ll
#define MOD 1000000007ll
#define REP(i,a,b) for(int i=(a);i<(b);++i)
#define RREP(i,n)  
#define rep(i,n) for(int i=n-1;i>=0;--i)
#define COUT(str) cout << str << endl


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; }
inline int toInt(string s) { int v; istringstream sin(s); sin >> v; return v; }
template<class T> inline string toString(T x) { ostringstream sout; sout << x; return sout.str(); }
template<class T> inline T sqr(T x) { return x*x; }

int N;

//------------------------------------------------------------------------------------------------------

int main() {
	cin >> N;

	if (N == 3 or N == 5 or N == 7) {
		COUT("YES");
	}

	else {
		COUT("NO");
	}


	return 0;
}


Measured memory_bytes: 256
import qualified Data.ByteString.Char8 as BS
import Control.Monad
import Control.Monad.ST
import Data.List
import Data.Array.ST
import Data.Array
import Data.Maybe (fromJust)


solve :: Int -> Int -> Int
solve a b = (a + b) `mod` 24


main :: IO ()
main = do
    (a:b:_) <- map (fst . fromJust . BS.readInt) . BS.words <$> BS.getLine
    print $ solve a b


Measured memory_bytes: 380
#include <stdio.h>

long long int x;
long long ans;

int main(void)
{
	scanf("%lld", &x);
	// (x / (6 + 5) + 1) * 2
	if (x % (6 + 5) != 0)
	{
		if ((6 + 5)*(x / (6 + 5)+1) >= x + 5)
		{
			printf("%lld\n", (x / (6 + 5)+1) * 2 - 1);
		}
		else
		{
			printf("%lld\n", (x / (6 + 5)+1) * 2);
		}
	}
	else
	{
		if ((6 + 5)*(x / (6 + 5)) >= x + 5)
		{
			printf("%lld\n", (x / (6 + 5)) * 2 - 1);
		}
		else
		{
			printf("%lld\n", (x / (6 + 5)) * 2);
		}
	}
	return 0;
}

Measured memory_bytes: 128
#include<bits/stdc++.h>
using namespace std;
#define LL long long
#define M 100005
int n,A[M];
int Ans;
int main() {
	scanf("%d",&n);
	for(int i=1; i<=n; i++)scanf("%d",A+i);
	sort(A+1,A+1+n);
	printf("%d",A[n/2+1]-A[n/2]);
}

Measured memory_bytes: 640
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<queue>
#include<stack>
#define maxn 200010
using namespace std;
int read()
{
    int x=0,f=1;
    char ch=getchar();
    while(ch-'0'<0||ch-'0'>9){if(ch=='-') f=-1;ch=getchar();}
    while(ch-'0'>=0&&ch-'0'<=9){x=x*10+ch-'0';ch=getchar();}
    return x*f;
}
int n;
struct P{
    int l,r;
}a[maxn];
bool cmp(P a,P b)
{
    return a.r<b.r;
}
int main()
{
    n=read();
    for(int i=1;i<=n;i++)  a[i].l=read(),a[i].r=read();
    sort(a+1,a+n+1,cmp);
    int now=0;
    for(int i=1;i<=n;i++)
    {
        if(now+a[i].l>a[i].r)
        {
            puts("No");
            return 0;
        }
        now=now+a[i].l;
    }
    puts("Yes");
    return 0;
}

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

int main(){
	int N,W;
	cin>>N>>W;

	int v[1000],w[1000];
	for(int i=0;i<N;i++)
		cin>>v[i]>>w[i];

	int DP[101][10001];
	for(int j=0;j<=W;j++)
		DP[0][j]=0;

	for(int i=1;i<=N;i++){
		for(int j=0;j<=W;j++){
			if(j<w[i-1])
				DP[i][j]=DP[i-1][j];
			else{
				if(DP[i-1][j]>DP[i-1][j-w[i-1]]+v[i-1])
					DP[i][j]=DP[i-1][j];
				else
					DP[i][j]=DP[i-1][j-w[i-1]]+v[i-1];
			}
		}
	}
	
	cout<<DP[N][W]<<endl;
	
	return 0;
}

	

Measured memory_bytes: 7008
#include <bits/stdc++.h>
using namespace std;
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n)  FOR(i,0,n)
#define ALL(a)  (a).begin(),(a).end()
#define SUM(a)  accumulate((a).begin(),(a).end(),0)
#define SORT(a) sort(ALL(a))
#define REV(a) reverse(ALL(a))
#define dump(x)  cerr << #x << " = " << (x) << endl;
typedef long long ll;
const int INTINF = 1<<29;
const ll LLINF = 1LL<<61;
const int MOD = (int)1e9 + 7;
const double EPS = 1e-9;
const int DX[8]={ 0, 1, 0,-1, 1, 1,-1,-1};
const int DY[8]={ 1, 0,-1, 0, 1,-1, 1,-1};

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  string a,b;
  cin >> a >> b;
  if (a==b){
      cout << "H" << endl;
  }else{
      cout << "D" << endl;
  }

}


Measured memory_bytes: 256
N,M,C = map(int,input().split())
B = list(map(int,input().split()))
AC = 0
for i in range(N):
    A = list(map(int,input().split()))
    x = C
    for j in range(M):
        x += A[j]*B[j]
    if x > 0:
        AC += 1
print(AC)

Measured memory_bytes: 3060
#!/usr/bin/env python3
from collections import defaultdict
from collections import deque
from heapq import heappush, heappop
import sys
import math
import bisect
import random
import itertools
sys.setrecursionlimit(10**5)
stdin = sys.stdin
bisect_left = bisect.bisect_left
bisect_right = bisect.bisect_right
def LI(): return list(map(int, stdin.readline().split()))
def LF(): return list(map(float, stdin.readline().split()))
def LI_(): return list(map(lambda x: int(x)-1, stdin.readline().split()))
def II(): return int(stdin.readline())
def IF(): return float(stdin.readline())
def LS(): return list(map(list, stdin.readline().split()))
def S(): return list(stdin.readline().rstrip())
def IR(n): return [II() for _ in range(n)]
def LIR(n): return [LI() for _ in range(n)]
def FR(n): return [IF() for _ in range(n)]
def LFR(n): return [LI() for _ in range(n)]
def LIR_(n): return [LI_() for _ in range(n)]
def SR(n): return [S() for _ in range(n)]
def LSR(n): return [LS() for _ in range(n)]
mod = 1000000007
inf = float('INF')

#A
def A():
    a = LS()
    if a[1][0] == "+":
        print(int("".join(a[0])) + int("".join(a[2])))
    else:
        print(int("".join(a[0])) - int("".join(a[2])))
    return

#B
def B():
    return

#C
def C():
    n = II()
    a = LI()
    d = defaultdict(int)
    ans = 1
    if n & 1:
        for i in a:
            if i & 1:
                print(0)
                return
            d[i] += 1
        for a, b in d.items():
            if a:
                if b == 2:
                    ans *= 2
                    ans %= mod
                    continue
                print(0)
                return
            if b == 1:
                continue
            print(0)
            return
    else:
        for i in a:
            if i & 1:
                d[i] += 1
                continue
            print(0)
            return
        for a, b in d.items():
            if b == 2:
                ans *= 2
                ans %= mod
                continue
            print(0)
            return
    print(ans)
            
        
    return

#D
def D():
    return


#Solve
if __name__ == '__main__':
    C()


Measured memory_bytes: 56416
#include<stdio.h>
int main(){
int a,b,c,i;
scanf("%d %d %d",&a,&b,&c);
if(a>b){
i=a; a=b; b=i;
}
if(b>c){
i=b; b=c; c=i;
}
if(a>b){
i=a; a=b; b=i;
}
printf("%d %d %d\n",a,b,c);
}


Measured memory_bytes: 2076
N = gets.to_i
p = gets.split.map(&:to_i)

ans = 0
N.times do |i| 
  
  if p[i] == i+1
    ans += 1
    p[i], p[i+1] = p[i+1], p[i] if i != N-1
  end
end

puts ans

Measured memory_bytes: 10116
#include <iostream>
#include <string>
#include <algorithm>
#include <vector>
#include <cmath>
#include <queue>
#include <map>
#include <unordered_map>
#include <set>
#include <functional>
#include <bitset>
#include <cassert>
#include <random>
using namespace std;
using ll = long long;


int main() {
	int n; cin >> n;
	int cnt = 0;
	for (int i = 0; i < n; i++) {
		int a; cin >> a;
		if (a == cnt + 1) cnt++;
	}
	if (cnt > 0) cout << n - cnt << endl;
	else cout << -1 << endl;
	return 0;
}


Measured memory_bytes: 384
import numpy as np
import sys
input = sys.stdin.readline
N = int(input())
A = [int(x) for x in input().split()]
A_count_mynus =[i for i in A if i<0]
A_plus =[abs(i) for i in A]

#Aの内マイナスの要素数が偶数奇数で場合分け
ans =0
if len(A_count_mynus) %2 ==0:
    ans =sum(A_plus) #好きなだけ－1を乗じれば全て正
else:
    A_plus.sort()
    ans = -A_plus[0] +sum(A_plus[1:]) #最小値のみマイナス、他はプラス
print(ans)

Measured memory_bytes: 23372
#include "bits/stdc++.h"

#define rep(i, n) for(int i = 0; i < (int)(n); i++)
#define all(x) (x).begin(),(x).end()
#define bit(n) (1LL << (n))
#define sz(x) ((int)(x).size())
#define UNIQUE(v) v.erase( unique(v.begin(), v.end()), v.end() );
#define en '\n'
#define pb push_back
#define P pair<int, int>

typedef long long ll;
using namespace std;

ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
ll lcm(ll a, ll b) { ll g = gcd(a, b); return a / g * b; }
const int dx[4] = { 1, 0, -1,  0 };
const int dy[4] = { 0, 1,  0, -1 };
const int INF = (int)1e9;
const ll  inf = (ll)1e18;
const int mod = 1000000007;

const double pi = 3.1415926535;

int main() {
	ll n;
	ll X;
	cin >> n >> X;
	vector<ll> x(n);
	rep(i, n) {
		cin >> x[i];
	}
	x.pb(X);
	sort(all(x));

	vector<ll> a(n + 1);
	for(int i = 0; i < n; i++) {
		a[i] = x[i + 1] - x[i];
	}

	ll g = a[0];
	for (int i = 1; i < n; i++) {
		g = gcd(g, a[i]);
	}

	cout << g << en;
}

Measured memory_bytes: 1792
#include<bits/stdc++.h>
using namespace std;
int main(){
  int h,w;
  int c=0;
  cin>>h>>w;
  string s[h];
  for(int i=0;i<h;i++) cin>>s[i];
  for(int i=0;i<h;i++){
    int a=0;
    for(int j=0;j<w;j++){
      if(s[i].at(j)=='#')
        a++;
    }
    if(a==0){
      string t=s[i];
      for(int k=i-1;k>=0;k--){
        s[k+1]=s[k];
      }
      s[0]=t;
      c++;
    }
  }
  for(int i=w-1;i>=0;i--){
    int d=0;
    for(int j=0;j<h;j++){
      if(s[j].at(i)=='#')
        d++;
    }
    if(d==0){
      for(int l=0;l<h;l++)
        s[l].erase(i,1);
    }
  }
  for(int i=c;i<h;i++)
  cout<<s[i]<<endl;
}

Measured memory_bytes: 256
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MOD=1e9+7;
const int INF=1e9;

int main(){
    int n,a,b;
    cin>>n>>a>>b;
    cout<<min(a,b)<<" "<<max(0,a+b-n)<<endl;
}


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

 

int main()
{
    int card[52];
    int n, i, rank, s;
    char suit;
    
    //initialize all card to be 0
    for(i=0; i<52; i++)
        card[i] = 0;
        
    cin >> n;
    for(i=1; i<=n; i++) //total 47 times in the example
    {
        cin >> suit;
        cin >> rank;
        if(suit == 'S')
            s = 0;
        else if(suit == 'H')
            s = 13;
        else if(suit == 'C')
            s = 26;
        else
            s = 39;
        
        s = s + rank - 1;
        card[s] = 1;
    }
    
    for(i=0; i<52; i++)
    {
        if(card[i]==0)
        {
            if(i >= 39)
            {
                suit = 'D';
                rank = i - 39 + 1;
            }
            else if(i >= 26)
            {
                suit = 'C';
                rank = i - 26 + 1;
            }
            else if(i >= 13)
            {
                suit = 'H';
                rank = i - 13 + 1;
            }
            else
            {
                suit = 'S';
                rank = i + 1;
            }
            cout << suit << " " << rank << endl;
        }
    }
}


Measured memory_bytes: 3092
#include <cstdlib>
#include <iostream>
#include <queue>
#include <vector>
using namespace std;

#define REP(i, a, b) for(int i = (a); i < int(b); ++i)
#define rep(i, n) REP(i, 0, n)

constexpr int WIDTH = 5;
constexpr int EMPTY = 0;
constexpr int NUM = 3;

int disappear(vector<vector<int>> &board) {
	const int h = board.size();

	int res = 0;
	rep(y, h) {
		int l = 0;

		while(l <= WIDTH - NUM) {
			int r = l + 1;
			while(r < WIDTH && board[y][l] == board[y][r]) {
				++r;
			}

			if(r - l >= NUM) {
				res += (r - l) * board[y][l];
				REP(x, l, r) board[y][x] = EMPTY;
			}

			l = r;
		}
	}
	return res;
}

void fall(vector<vector<int>> &board) {
	const int h = board.size();

	rep(x, WIDTH) {
		queue<int> que;
		for(int y = h - 1; y >= 0; --y) {
			if(board[y][x]) que.push(board[y][x]);
		}

		for(int y = h - 1; y >= 0; --y) {
			if(que.empty()) {
				board[y][x] = EMPTY;
			}
			else {
				board[y][x] = que.front();
				que.pop();
			}
		}
	}
}

int solve(vector<vector<int>> &board) {
	int score = 0;
	int point;

	while((point = disappear(board))) {
		score += point;
		fall(board);
	}
	return score;
}

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

	for(int h; cin >> h && h;) {
		vector<vector<int>> board(h, vector<int>(WIDTH));
		for(auto &line : board) for(auto &e : line) cin >> e;
		cout << solve(board) << endl;
	}
	return EXIT_SUCCESS;
}

Measured memory_bytes: 1216
#include<bits/stdc++.h>
using namespace std;
const int maxn=1010,mod=1e9+7;
int n,m,a[maxn],b[maxn],ans=1;
int main(){
	scanf("%d%d",&n,&m);
	for(int i=1;i<=n;i++) scanf("%d",a+i);
	for(int i=1;i<=m;i++) scanf("%d",b+i);
	sort(a+1,a+n+1);sort(b+1,b+m+1);
	for(int i=1;i<n;i++) if(a[i]==a[i+1]) return puts("0"),0;
	for(int i=1;i<m;i++) if(b[i]==b[i+1]) return puts("0"),0;
	if(a[n]!=n*m || b[m]!=n*m) return puts("0"),0;
	for(int i=n*m;i;i--){
		int s1=lower_bound(a+1,a+n+1,i)-a,s2=lower_bound(b+1,b+m+1,i)-b;
		if(a[s1]==i && b[s2]==i);
		else if(a[s1]==i) ans=1ll*ans*(m-s2+1)%mod;
		else if(b[s2]==i) ans=1ll*ans*(n-s1+1)%mod;
		else ans=1ll*ans*((n-s1+1)*(m-s2+1)-(n*m-i))%mod;
	}
	printf("%d\n",ans);
}

Measured memory_bytes: 384
#include <iostream>
#include <string>
#include <vector>
#include <map>
#include <set>
#include <list>
#include <algorithm>
#include <cmath>
#include <iomanip>
 
 
int main()
{
    int a, b, c; std::cin >> a >> b >> c;
    
    for(int i=0; i<b; i++)
    {
        if((a*i)%b == c)
        {
            std::cout << "YES" << std::endl;
            return 0;
        }
    }
    std::cout << "NO" << std::endl;
    return 0;
}

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

signed main(void)
{
	int n, maxi, ans;
	cin >> n;
	maxi = 0;
	rep(i, n)
	{
		int a, b;
		cin >> a >> b;
		if (a > maxi)
		{
			maxi = a;
			ans = a + b;
		}
	}
	cout << ans << endl;
	return 0;
}


Measured memory_bytes: 256
s = input()

for i in range(len(s)):
    # 奇数文字目
    if i % 2 ==0:
        if s[i] == 'L':
            print('No')
            exit()
    # 偶数文字目
    else:
        if s[i] == 'R':
            print('No')
            exit()
            
print('Yes')

Measured memory_bytes: 2940
#pragma GCC optimize(2)
#include<bits/stdc++.h>
#define ll long long
#define maxn 1000005
#define inf 1e9
#define eps 1e-10
using namespace std;

inline ll read()
{
	ll x=0,w=1; char c=getchar();
	while(c<'0'||c>'9') {if(c=='-') w=-1; c=getchar();}
	while(c<='9'&&c>='0') {x=(x<<1)+(x<<3)+c-'0'; c=getchar();}
	return w==1?x:-x;
}

ll dp[3005],n,s,a[3005];
const ll mod=998244353;

int main()
{
	n=read(); s=read();
	for(int i=1;i<=n;i++) a[i]=read();
	for(int i=1;i<=s;i++) dp[i]=0;
	ll ans=0;
	for(int i=1;i<=n;i++)
	{
		for(int j=s;j>=a[i];j--)
		{
			if(j==a[i]) dp[j]=(dp[j]+i)%mod;
			else dp[j]=(dp[j]+dp[j-a[i]])%mod;
		}
		ans=(ans+(n-i+1)*dp[s]%mod)%mod; dp[s]=0;
	}
	cout<<ans<<endl;
	return 0;
}

Measured memory_bytes: 256
#include<iostream>
#include<cstdio>
#include<cmath>
#include<vector>
#include<list>
#include<set>
#include<map>
#include<stack>
#include<queue>
#include<string>
#include<cstring>
#include<utility>
#include<algorithm>
#include<functional>
#include<deque>
#include<ctype.h>
#include<time.h>
#include<stdlib.h>
using namespace std;


int main(){
   int n, a;
   cin >> n >> a;
   cout << ((n%500 <= a)? "Yes":"No") << endl;
}


Measured memory_bytes: 256
s,t=list(input()),list(input())
length_s,length_t=len(s),len(t)
flag=0
for i in reversed(range(length_s-length_t+1)):
    if flag==1:break
    word=s[i:i+length_t]
    cnt=0
    for j in range(length_t):
        if word[j]==t[j] or word[j]=="?":
            cnt +=1
        if cnt==length_t:
            flag=1
            s[i:i+length_t]=t
s="".join(s).replace("?","a")
print(s if flag else "UNRESTORABLE")

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

#define REP(i,n) for(int (i)=0;i<(n);i++)
#define vi vector<int>

using namespace std;

int main()
{
  int n, m;
  cin >> n >> m;
  n++;
  vi v(n);
  REP(i, n) v[i] = 1;

  int aka = 200000;
  v[1] = aka;
  int x, y;
  REP (i, m) {
    cin >> x >> y;
    if (aka <= v[x])
    {
      v[x] == aka ? v[x] = 0 : v[x]--;
      v[y] < aka ? v[y] += aka : v[y]++;
    }
    else
    {
      v[x]--;
      v[y]++;
    }
  }

  int ans = 0;
  REP(i, n){
    ans += (aka <= v[i]);
  }
  cout << ans << endl;
  return 0;
}

Measured memory_bytes: 640
#include<iostream>
#include<string>
#include<vector>
#include<iomanip>
#include<algorithm>
#include<queue>
#include<stack>
#include<list>
#include<map>
#include<deque>
#include<cctype>
#include<math.h>
using namespace std;
#define ll long long
int main(){
    string S;
    cin >> S;
    ll i;
    ll ans=0;
    ll l=0,r=0;
    for(i=0;i<S.size();i++){
        if(S[i]=='<')l++;
        if(S[i]=='>')r++;
        if(i!=S.size()-1&&S[i]=='>'&&S[i+1]=='<'){
            ans+=l*(l+1)/2+r*(r+1)/2-min(l,r);
            l=0;
            r=0;
        }
    }
    ans+=l*(l+1)/2+r*(r+1)/2-min(l,r);
    cout << ans;
    return 0;
}


Measured memory_bytes: 900
#include<bits/stdc++.h>
#define fn(i,n) for(int i=0;i<n;i++)
#define rn(i,n )for(int i=1;i<n;i++)
#define ll  long long int
#define wh while
#define str string
#define pb push_back
#define N 2101
#define MOD 100000007
#define sc(n) scanf("%lld",&n)
#define mkp(a,b) make_pair(a,b)
#define INF 100000000000
using namespace std;
ll power(ll x, ll y, ll p)
{
    ll res = 1;   
    x = x % p; 
    while (y > 0)
    {
     
        if (y & 1)
            res = (res*x) % p;

        y = y>>1;
        x = (x*x) % p;  
    }
    return res;
}
vector < pair <ll,ll> > v;

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);cout.tie(NULL);
    ll k;cin>>k;
    ll c1=0;ll c2=0;
    rn(i,k+1)
    {
        if(i%2==0)
        c1++;
        else
        c2++;
    }
    cout<<c1*c2<<endl;
  
 
   return 0;
}

Measured memory_bytes: 256
#include<iostream>
using namespace std;
const long long mod=1e9+7;
long long h,w,a,b,ans,jc[222222],injc[222222];
long long binpow(long long a,long long t)
{
	long long res=1,p=a;
	for (long long i=t;i;i>>=1)
	{
		if (i&1) res=res*p%mod;
		p=p*p%mod;
	}
	return res;
}
void Init()
{
	jc[0]=1;injc[0]=1;
	for (int i=1;i<=200000;i++)
	{
		jc[i]=jc[i-1]*i%mod;
		injc[i]=injc[i-1]*binpow(i,mod-2)%mod;
	}
}
long long C(int a,int b)
{
	return ((jc[b]*injc[a])%mod)*injc[b-a]%mod;
}
int main()
{
	cin>>h>>w>>a>>b;
	Init();
	ans=C(h-1,h+w-2);
	for (int i=1;i<=b;i++)
	{
		ans-=(C(h-a-1,h-a+i-2)*C(a-1,w-i+a-1)%mod);
		if (ans<0) ans+=mod;
	}
	if (ans<0) ans+=mod;
	cout<<ans<<endl;
	return 0;
}

Measured memory_bytes: 3328
#include <iostream>
#include <stdio.h>
#include <vector>
#include <cstring>
#include <set>
#include <map>
#include <unordered_set>
#include <unordered_map>
#include <utility>
#include <queue>
#include <deque>
#include <stack>
#include <time.h>
#include <stdlib.h>
#include <bitset>
#include <algorithm>

#include <chrono>

#define INF 1e9
#define LL_INF 1e18

#define fst first
#define snd second
#define pb push_back
#define SZ(x) ((int)(x.size()))

#define MOD1 998244353
#define MOD2 666013
#define P1 255
#define P2 2059

using namespace std;

typedef long long int ll;

typedef pair<ll, ll> pll;
typedef pair<int, int> pii;

#define nmax 1010
#define MOD 1000000007

int n, m;
int dp[nmax][nmax];
char s[nmax][nmax];

int f(int x)
{
    if (x >= MOD) return x - MOD; else
        return x;
}

int main()
{
    scanf("%d %d", &n, &m);

    for (int i = 1; i <= n; i++) scanf("%s", s[i] + 1);

    dp[0][1] = 1;

    for (int i = 1; i <= n; i++)
        for (int j = 1; j <= m; j++)
            if (s[i][j] == '.') dp[i][j] = f(dp[i - 1][j] + dp[i][j - 1]);

    printf("%d", dp[n][m]);

    // IMPORTANT!!!!!
    // Are you missing something????
    // check limits, int or ll

    return 0;
}

Measured memory_bytes: 5248
import sys
read = sys.stdin.buffer.read
input = sys.stdin.readline
input = sys.stdin.buffer.readline


#sys.setrecursionlimit(10**9)
#from functools import lru_cache

def RD(): return sys.stdin.read()
def II(): return int(input())
def MI(): return map(int,input().split())
def MF(): return map(float,input().split())
def LI(): return list(map(int,input().split()))
def LF(): return list(map(float,input().split()))
def TI(): return tuple(map(int,input().split()))
# rstrip().decode('utf-8')

#2245
#import numpy as np

def main():
	n=II()
	print(n*(n-1)//2)


if __name__ == "__main__":
	main()

Measured memory_bytes: 3064
#include<stdio.h>
int main()
{
    char ch[20000];
    int i;
   scanf("%s",ch);
   for(i=1;ch[i]!='\0';i++)
    {
        if(ch[i]==',')
        {
            ch[i]=' ';
        }
    }
    printf("%s\n",ch);
        

    return 0;
    }

Measured memory_bytes: 128
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<algorithm>

int d[5];

int main()
{
	for (int i=1;i<=3;i++)
	{
		int x,y;scanf("%d%d",&x,&y);
		d[x]++;d[y]++;
	}
	int mx=0;
	for (int i=1;i<=4;i++) mx=std::max(mx,d[i]);
	if (mx>2) puts("NO");
	else puts("YES");
	return 0;
}

Measured memory_bytes: 256
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<cstdio>
#include<string>
#include<algorithm>
#include<cmath>
using namespace std;

#define REP(i,n) for(int i=0;(i)<(n);(i)++)

long long gcd(long long x, long long y) {
	long long r;
	if (x < y) swap(x, y);

	while (y > 0) {
		r = x%y;
		x = y;
		y = r;
	}
	return x;
}

long long lcm(long long x, long long y) {
	return x / gcd(x, y)*y;
}

int main() {
	int N;
	long long T[102];
	cin >> N;
	
	REP(i, N) {
		cin >> T[i];
	}
	
	long long ans;
	ans = 1;
	for (int i = 0;i < N;i++) {
		ans = lcm(T[i], ans);
	}

	cout << ans << endl;
	return 0;
}

Measured memory_bytes: 256
#include <iostream>
#include <cmath>
#include <string>
#include <cstdlib>
#include <iomanip>
#include <sstream>
#include <cstdio>
#include <vector>
#include <algorithm>
#include <cctype>
#include <map>
#include <cstring>
#include <typeinfo>
#include <utility>
#define REP(i,n) for(int i = 0;i < n ;i++)
#define FOR(i,m,n) for(int i= m ;i<n;i++)
using namespace std;

int main(void){
  int n;
  cin >> n;
  int i,j;
  int sum=0;
  int tmp;
  vector<int>t(n);
  for(i=0;i<n;i++){
    cin >> t[i];
  }
  int m;
  cin >> m;
  vector<int> p(m),x(m);
  for(i=0;i<m;i++){
    cin >> p[i] >> x[i];
    tmp=t[p[i]-1];
    t[p[i]-1]=x[i];
    for(j=0;j<n;j++){
      sum+=t[j];
    }
    cout << sum << endl;
    sum=0;
    t[p[i]-1]=tmp;
  }

  return 0;
}


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

using namespace std;

#define FOR(i, m, n) for (int i = m; i < n; ++i)
#define FORD(i, m, n) for (int i = m; i >= n; --i)
#define REP(i, n) for (int i = 0; i < n; ++i)
#define REP1(i, n) for (int i = 0; i <= n; ++i)
#define REPR(i, n) for (int i = n; i >= 0; --i)
#define EPS (1e-7)
#define INF (1e9)
#define PI (acos(-1))

using ll = long long;
using ull = unsigned long long;
using ld = long double;

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

	string s;
	cin>>s;
	int len=s.length();
	if(s[len-1]!='s')s=s+"s";
	else s=s+"es";
	cout << s << endl;

	return 0;
}

Measured memory_bytes: 3656
# -*- coding:utf-8 -*-
import sys

"""
Bs[i] >= max(As[i], As[i+1]) を満たす条件で、

sum(As)の最大値を求める問題
"""

def solve():
    N = int(sys.stdin.readline())
    Bs = list(map(int, sys.stdin.readline().split()))

    if N == 2:
        print(Bs[0]*2)
        return

    # 初期値を決める
    As = [0] * N
    if Bs[0] <= Bs[1]:
        As[0] = As[1] = Bs[0]
    else:
        As[0], As[1] = Bs[0], Bs[1]

    # 貪欲法
    for i in range(1, N-2):
        if Bs[i] > Bs[i+1]:
            # As:   ?    0
            # Bs: 153    0
            As[i+1] = Bs[i+1]
        else:
            # As:   ?  153
            # Bs: 153  160
            As[i+1] = Bs[i]
    As[N-1] = Bs[N-2]

    print(sum(As))



if __name__ == "__main__":
    solve()


Measured memory_bytes: 3064
#include<bits/stdc++.h>
#define rep(i,x,y) for (int i=(x);i<=(y);i++)
using namespace std;
const int N=311,mod=998244353;
int n,a[N],f[N][N*N],g[N][N*N],all,sum,res;
inline void upd(int &x,int y){x+=y; x-=x>=mod?mod:0;}
 int main(){
    ios::sync_with_stdio(0);
    cin>>n;
    all=1;
    rep (i,1,n) cin>>a[i],all=all*3ll%mod;
    f[0][0]=1;
    rep (i,1,n){
        rep(j,0,sum) 
		if(f[i-1][j]){
        upd(f[i][j],f[i-1][j]*2ll%mod);
        upd(f[i][j+a[i]],f[i-1][j]);
        }
        sum+=a[i];
    }
    rep (i,(sum+1)>>1,sum) upd(res,f[n][i]);
    if (!(sum&1)){
        sum=0;
        g[0][0]=1;
        rep (i,1,n){
            rep (j,0,sum) if (g[i-1][j]){
            upd(g[i][j],g[i-1][j]);
			upd(g[i][j+a[i]],g[i-1][j]);
            }
            sum+=a[i];
        }
        upd(res,mod-g[n][sum/2]);
    }
    upd(all,(mod-res)*3ll%mod);
    cout<<all;
    return 0;
 }

Measured memory_bytes: 227584