akhauriyash/Code-Regression huggingface dataset:
#include<iostream>
#include<stack>
#include<vector>
#include<algorithm>
#include<cstring>
#define rep(i,n) for(int i=0;i<n;i++)
using namespace std;

typedef vector<int> pos;
typedef pair<int,int> P;

int w,h;
string g[16];
int dy[] = {-1,0,1,0}, dx[] = {0,1,0,-1};
bool vis[16][64][4][16][64][4];

int main(){
  int cnt=0;
  while(cin >> w >> h,w){
    cnt++;
    rep(i,h)cin >> g[i];
    memset(vis,0,sizeof(vis));

    int sy,sx,sd, gy,gx,gd;
    rep(i,h){
      rep(j,w){
	if(g[i][j] == 'K'){
	  rep(k,4){
	    int ny = i+dy[k], nx = j+dx[k];
	    if(ny<0 || nx<0 || ny>=h || nx>=w)continue;
	    if(g[ny][nx] == '.'){
	      sy = i; sx = j; sd = k;
	    }
	  }
	}
	if(g[i][j] == 'M'){
	  rep(k,4){
	    int ny = i+dy[k], nx = j+dx[k];
	    if(ny<0 || nx<0 || ny>=h || nx>=w)continue;
	    if(g[ny][nx] == '.'){
	      gy = i; gx = j; gd = k;
	    }
	  }
	}
      }
    }

    stack<pos> s;
    pos p(6);
    p[0] = sy; p[1] = sx; p[2] = sd;
    p[3] = sy; p[4] = sx;

    rep(i,4){
      vis[sy][sx][sd][sy][sx][i] = true;
      p[5] = i;
      s.push(p);
    }

    while(s.size()){
      p = s.top(); s.pop();

      int d1 = p[2], y1 = p[0]+dy[d1], x1 = p[1]+dx[d1];
      while(g[y1][x1]!='#'){
	y1+=dy[d1]; x1+=dx[d1];
      }
      y1-=dy[d1]; x1-=dx[d1];

      int d2 = p[5], y2 = p[3], x2 = p[4];
      while(g[y2][x2]!='#'){
	int rd = (p[5]+1)%4, ry = y2+dy[rd], rx = x2+dx[rd];
	int ld = (p[5]+3)%4, ly = y2+dy[ld], lx = x2+dx[ld];

	if(g[ry][rx]=='#' || g[ly][lx]=='#'){
	  if(!vis[y1][x1][d1][y2][x2][d2]){
	    vis[y1][x1][d1][y2][x2][d2] = true;
	    if(g[ry][rx]=='#'){
	      pos tmp(6);
	      tmp[0] = y1; tmp[1] = x1; tmp[2] = (d1+1)%4;
	      tmp[3] = y2; tmp[4] = x2; tmp[5] = rd;
	      s.push(tmp);
	    }

	    if(g[ly][lx]=='#'){
	      pos tmp(6);
	      tmp[0] = y1; tmp[1] = x1; tmp[2] = (d1+3)%4;
	      tmp[3] = y2; tmp[4] = x2; tmp[5] = ld;
	      s.push(tmp);
	    }
	  }
	}
	y2-=dy[d2]; x2-=dx[d2];
      }
    }
    bool f1=false;
    rep(i,4){
      if(vis[gy][gx][i][gy][gx][gd])f1 = true;
    }

    if(f1)cout << "He can accomplish his mission." << endl;
    else{
      memset(vis,0,sizeof(vis));
      p.resize(3);
      p[0] = sy; p[1] = sx; p[2] = sd;
      s.push(p);

      while(s.size()){
	p = s.top(); s.pop();
	
	int ny = p[0]+dy[p[2]], nx = p[1]+dx[p[2]];
	while(g[ny][nx]!='#'){
	  ny += dy[p[2]]; nx += dx[p[2]];
	}
	ny -= dy[p[2]]; nx -= dx[p[2]];
	
	if(vis[ny][nx][p[2]][0][0][0])continue;
	vis[ny][nx][p[2]][0][0][0] = true;
	rep(d,2){
	  int nd = (p[2] + 2*d+1)%4;
	  pos tmp(3);
	  tmp[0] = ny; tmp[1] = nx; tmp[2] = nd;
	  s.push(tmp);
	}
      }	  
      
      bool f2=false;
      rep(i,4){
	if(vis[gy][gx][i][0][0][0])f2 = true;
      }
      if(f2)cout << "He cannot return to the kitchen." << endl;
      else cout << "He cannot bring tea to his master." << endl;
    }
  }
}

Measured memory_bytes: 18032
#pragma GCC optimize("O3")
#include <bits/stdc++.h>
#define ll long long
#define rep(i,n) for(ll i=0;i<(n);i++)
#define pll pair<ll,ll>
#define pq priority_queue
#define pb push_back
#define eb emplace_back
#define fi first
#define se second
#define ios ios_base::sync_with_stdio(0),cin.tie(0),cout.tie(0);
#define lb(c,x) distance(c.begin(),lower_bound(all(c),x))
#define ub(c,x) distance(c.begin(),upper_bound(all(c),x))

using namespace std;

template<class T> inline bool chmax(T& a,T b){if(a<b){a=b;return 1;}return 0;}
template<class T> inline bool chmin(T& a,T b){if(a>b){a=b;return 1;}return 0;}

const ll mod=998244353;

int main(){
    ll h,w,d;
    cin >> h >> w >> d;
    vector<vector<ll>> a(h,vector<ll>(w));
    vector<vector<pair<ll,pll>>> v(d);
    vector<vector<ll>> sum(d);
    rep(i,h){
        rep(j,w){
            cin >> a[i][j];
            pll s=make_pair(i,j);
            v[a[i][j]%d].push_back(make_pair(a[i][j],s));
        }
    }
    rep(i,d){
        sort(v[i].begin(),v[i].end());
        rep(j,v[i].size()){
            if(j==0){
                sum[i].push_back(0LL);
            }
            if(j){
                sum[i].push_back(sum[i][j-1]+abs(v[i][j].se.fi-v[i][j-1].se.fi)+abs(v[i][j].se.se-v[i][j-1].se.se));
            }
        }
    }
    ll q;
    cin >> q;
    rep(i,q){
        ll le,ri;
        cin >> le >> ri;
        ll p=(le-1)/d;
        ll q=le%d;
        ll r=(ri-1)/d;
        cout << sum[q][r]-sum[q][p] << endl;
    }
} 

Measured memory_bytes: 13704
import java.util.Arrays;
import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int [] i = {sc.nextInt(),sc.nextInt(),sc.nextInt()};
		Arrays.sort(i);
		System.out.println(i[0]+i[1]);
	}
}


Measured memory_bytes: 21844
#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define pb push_back
#define ff first
#define ss second
vector<vector<int>> v;
vector<bool> vi;
void dfs(int r)
{
 if(vi[r])
 return;
 vi[r]=true;
 for(auto it:v[r])
 {
  if(!vi[it])
  dfs(it);
 }
}
int main(void){
    // Your code here!
 ios_base::sync_with_stdio(false);
 cin.tie(NULL);
 int i,c,m,n,x,y;
 c=0;
 cin >> n >> m;
 v.resize(n);
 vi.resize(n,false);
 while(m--)
 {
  cin >> x >> y;
  v[x-1].pb(y-1);
  v[y-1].pb(x-1);
 }
 for(i=0;i<n;i++)
 {
  if(!vi[i])
  {
   dfs(i);
   c++;
  }
 }
 cout << c-1;
 return 0;
}

Measured memory_bytes: 9208
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int n,K;
ll calc(ll x,int now){
	ll ans=0;
	for(ll i,j=now;j<x;){
		i=j+1;j=x/(x/i);
		ans+=(x/i)*(j-i+1);
	}
	return ans;
}
int main(){
	scanf("%d%d",&n,&K);
	ll ans=0;
	for(int i=K;i<=n;i++){
		ans+=calc(n-i,i);
		if(i) ans+=(n-i);
	}
	printf("%lld",ans);
	return 0;
}

Measured memory_bytes: 256
#082_D
s = input()
x, y = map(int, input().split())
xgo, ygo = [], []

cnt = 0
flg = True
check = True if s[0] == 'F' else False
for i in range(len(s)):
    if s[i] == 'T':
        if flg and cnt > 0:
            xgo.append(cnt)
        elif cnt > 0:
            ygo.append(cnt)
        cnt = 0
        flg = (flg + 1) % 2
    else:
        cnt += 1

if flg and cnt > 0:
    xgo.append(cnt)
elif cnt > 0:
    ygo.append(cnt)

flg1, flg2 = len(xgo)==0, len(ygo)==0

sum_x, sum_y = sum(xgo), sum(ygo)
dp1 = [[False for _ in range(2*sum_x+1)] for _ in range(len(xgo)+1)]
dp2 = [[False for _ in range(2*sum_y+1)] for _ in range(len(ygo)+1)]

if check:
    dp1[1][sum_x+xgo[0]] = True
    for i in range(2, len(xgo)+1):
        for j in range(-sum_x, sum_x+1):
            if 0 <= j+sum_x-xgo[i-1] <= 2*sum_x:
                if dp1[i-1][j+sum_x-xgo[i-1]]:
                    dp1[i][j+sum_x] = True
            if 0 <= j+sum_x+xgo[i-1] <= 2*sum_x:
                if dp1[i-1][j+sum_x+xgo[i-1]]:
                    dp1[i][j+sum_x] = True
else:
    dp1[0][sum_x] = True
    for i in range(1, len(xgo)+1):
        for j in range(-sum_x, sum_x+1):
            if 0 <= j+sum_x-xgo[i-1] <= 2*sum_x:
                if dp1[i-1][j+sum_x-xgo[i-1]]:
                    dp1[i][j+sum_x] = True
            if 0 <= j+sum_x+xgo[i-1] <= 2*sum_x:
                if dp1[i-1][j+sum_x+xgo[i-1]]:
                    dp1[i][j+sum_x] = True

dp2[0][sum_y] = True
for i in range(1, len(ygo)+1):
    for j in range(-sum_y, sum_y+1):
        if 0 <= j+sum_y-ygo[i-1] <= 2*sum_y:
            if dp2[i-1][j+sum_y-ygo[i-1]]:
                dp2[i][j+sum_y] = True
        if 0 <= j+sum_y+ygo[i-1] <= 2*sum_y:
            if dp2[i-1][j+sum_y+ygo[i-1]]:
                dp2[i][j+sum_y] = True
                
if flg1:
    flgx = (x == 0)
elif abs(x) > sum_x:
    flgx = False
else:
    flgx = dp1[len(xgo)][sum_x+x]
    
if flg2:
    flgy = (y == 0)
elif abs(y) >sum_y:
    flgy = False
else:
    flgy = dp2[len(ygo)][sum_y+y]
    
print('Yes' if flgx and flgy else 'No')

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

#define rep(i, n) for (int i = 0; i < int(n); i++)
#define rrep(i, n) for (int i = int(n) - 1; i >= 0; i--)
#define reps(i, n) for (int i = 1; i <= int(n); i++)
#define rreps(i, n) for (int i = int(n); i >= 1; i--)
#define repc(i, n) for (int i = 0; i <= int(n); i++)
#define rrepc(i, n) for (int i = int(n); i >= 0; i--)
#define repi(i, a, b) for (int i = int(a); i < int(b); i++)
#define repic(i, a, b) for (int i = int(a); i <= int(b); i++)
#define each(x, y) for (auto &x : y)
#define all(a) (a).begin(), (a).end()
#define bit32(x) (1 << (x))
#define bit64(x) (1ll << (x))
#define sz(v) ((int) v.size())

using namespace std;

using i64 = long long;
using f80 = long double;
using vi32 = vector<int>;
using vi64 = vector<i64>;
using vf80 = vector<f80>;
using vstr = vector<string>;

inline void yes() { cout << "Yes" << endl; exit(0); }
inline void no() { cout << "No" << endl; exit(0); }
inline i64 gcd(i64 a, i64 b) { if (min(a, b) == 0) return max(a, b); if (a % b == 0) return b; return gcd(b, a % b); }
inline i64 lcm(i64 a, i64 b) { if (min(a, b) == 0) return max(a, b); return a / gcd(a, b) * b; }
template <typename T> class pqasc : public priority_queue<T, vector<T>, greater<T>> {};
template <typename T> class pqdesc : public priority_queue<T, vector<T>, less<T>> {};
template <typename T> inline void amax(T &x, T y) { x = max(x, y); }
template <typename T> inline void amin(T &x, T y) { x = min(x, y); }
template <typename T> inline T exp(T x, i64 n, T e = 1) { T r = e; while (n > 0) { if (n & 1) r *= x; x *= x; n >>= 1; } return r; }
template <typename T> istream& operator>>(istream &is, vector<T> &v) { each(x, v) is >> x; return is; }
template <typename T> ostream& operator<<(ostream &os, vector<T> &v) { rep(i, v.size()) { if (i) os << ' '; os << v[i]; } return os; }
void solve(); int main() { ios::sync_with_stdio(0); cin.tie(0); cout << fixed << setprecision(16); solve(); return 0; }

void solve() {
  int n, m;
  cin >> n >> m;
  if (n > m) swap(n, m);
  if (n == 1) {
    if (m == 1) {
      cout << 1 << endl;
    } else {
      cout << m - 2 << endl;
    }
    return;
  }
  cout << (i64) (n - 2) * (m - 2) << endl;
}


Measured memory_bytes: 256
K = int(input())
S = input()
S = len(S)


mod = 10**9 + 7

b = mod-2
blis = []
c = 0
while b >0:
  if b & 1 == 1:
    blis.append(c)
  c += 1
  b >>= 1

def modinv(a):
  if a == 1:
    return 1
  else:
    res = 1
    li = []
    for _ in range(c):
      li.append(a%mod)
      a = a*a%mod
    for item in blis:
      res = res *li[item] %mod
    return res

from collections import deque

fact = [1, 1]  # fact[n] = (n! mod p)
factinv = [1, 1]  # factinv[n] = ((n!)^(-1) mod p)
inv = [0, 1]  # factinv 計算用

for i in range(2, K + S + 1):
  fact.append((fact[-1] * i) % mod)
  if i < K+2:
    inv.append((-inv[mod % i] * (mod // i)) % mod)
    factinv.append((factinv[-1] * inv[-1]) % mod)

factinv_k = deque(factinv[:K+1])
fact_ks = deque(fact[S-1:])


list26 = deque([1])
list25 = deque([1])
now26 = 26
now25 = 25
for k in range(K):
  list25.append(now25)
  list26.append(now26)
  now25 = now25*25%mod
  now26 = now26*26%mod


ans = 0
for k in range(K+1):
  now = list26.pop()
  now = now * list25.popleft() % mod
  now = now * fact_ks.popleft() % mod
  now = now * factinv_k.popleft() %mod
  ans += now
  ans %= mod


ans *= modinv(fact[S-1])
ans %= mod
print(ans)


Measured memory_bytes: 249048
#include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int i = 0; i < (int)(n); i++)
#define repr(i,n) for(int i = (int)(n); i >= 0; i--)
#define all(v) v.begin(),v.end()
typedef long long ll;

int main(){
    int N;
    cin >> N;
    string S;
    cin >> S;
    int len = S.size();
    vector<string> letter(26);
    letter[0] = "A";
    letter[1] = "B";
    letter[2] = "C";
    letter[3] = "D";
    letter[4] = "E";
    letter[5] = "F";
    letter[6] = "G";
    letter[7] = "H";
    letter[8] = "I";
    letter[9] = "J";
    letter[10] = "K";
    letter[11] = "L";
    letter[12] = "M";
    letter[13] = "N";
    letter[14] = "O";
    letter[15] = "P";
    letter[16] = "Q";
    letter[17] = "R";
    letter[18] = "S";
    letter[19] = "T";
    letter[20] = "U";
    letter[21] = "V";
    letter[22] = "W";
    letter[23] = "X";
    letter[24] = "Y";
    letter[25] = "Z";
    rep(i,len){
        char moji = S[i];
        if (S[i] - 'A' + N >= 26){
            cout << letter[S[i] - 'A' + N - 26];
        }
        else{
            cout << letter[S[i] - 'A' + N];
        }
    }
    cout << endl;
}


Measured memory_bytes: 256
# gets.to_i
# gets.split.map(&:to_i)
# N.times.map{gets.to_i}
# N.times.map{gets.split.map(&:to_i)}
# 

#N,M=gets.split.map(&:to_i)

S=gets.chomp
if S=="AAA" || S=="BBB"
    puts "No"
else
    puts "Yes"
end

Measured memory_bytes: 1788
import java.util.*;

public class Main {

  public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    int n = sc.nextInt();
    String s = sc.next();
    char[] c = s.toCharArray();
    for (int i = 0; i < s.length(); i++) {
      c[i] += n;
      if (c[i] > 'Z') {
        c[i] -= 'Z';
        c[i] += 'A'-1;
      }
    }
    String k = new String(c);
    System.out.println(k);

  }

}


Measured memory_bytes: 23252
#include <cstdio>
#include <iostream>
#include <cmath>
#include <cstring>
#include <sstream>
#include <algorithm>
#include <cstdlib>
#include <map>
#include <queue>
#include <utility>
#include <vector>
#include <set>
#include <memory.h>
#include <iomanip>
#include <bitset>
#include <list>
#include <stack>
#include <deque>

using namespace std;

#define mod 1000000007

double result[3001][3001] = {};

int main()
{
	int n;
	cin >> n;
	double p[3010];
	for(int i = 1; i <= n; i++){
		cin >> p[i];
	}
	result[0][0] = 1.0;
	for(int i = 1; i <= n; i++){
		result[i][0] = result[i - 1][0] * (1.0 - p[i]);
		for(int j = 1; j <= i; j++){
			result[i][j] = result[i - 1][j] * (1.0 - p[i]) + result[i - 1][j - 1] * p[i];
		}
	}
	double ans = 0.0;
	for(int i = n /2 + 1; i <= n; i++){
		ans += result[n][i];
	}
	printf("%.12lf\n", ans);
}

Measured memory_bytes: 68992
import java.util.*;
public class Main {
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
int n=sc.nextInt();
int x=sc.nextInt();
int[] m=new int[n];
int sum=0,min=0;
for(int i=0;i<n;i++){
m[i]=sc.nextInt();
if(i==0){min=m[i];}else{min=Math.min(m[i],min);}
sum+=m[i];
}
int xx=x-sum;

System.out.println(n+xx/min);
}}

Measured memory_bytes: 23764
#include "bits/stdc++.h"
#include <set>

using namespace std;
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define RFOR(i, a, b) for (int i = (b)-1; i >= (a); i--)
#define rep(i, n) for (int i = 0; i < (n); i++)
#define rep1(i, n) for (int i = 1; i < (n); i++)
#define rrep(i, n) for (int i = (n)-1; i >= 0; i--)
#define all(i) (i).begin(), (i).end()
#define rall(i) (i).begin(), (i).end(), greater<int>()
#define int long long
#define elif else if
#define eb emplace_back
#define pb push_back
#define mp make_pair
#define fst first
#define sec second
#define mod 1000000007
#define INF 10e10
template <typename T>

void print(T x)
{
	std::cout << x << " ";
}
template <typename T>

void println(T x)
{
	std::cout << x << "\n";
}

signed main(void) {
	int n;
	cin >> n;
	vector<int>a(n);
	double sum = 0;
	rep(i, n) {
		cin >> a[i];
		sum += a[i];
	}
	sum /= n;
	int d = round(sum);
	int ans = 0;
	rep(i, n) {
		ans += pow((a[i] - d), 2);
	}
	println(ans);
}

Measured memory_bytes: 3628
#include<iostream>
#include<stdlib.h>
#include<algorithm>

using namespace std;
int main()
{
  int N,i;
  double result;
  cin>>N;
  if(N>=2 && N<=50)
  {
    int A[N];
  	for(i=0;i<N;i++)
  		cin>>A[i];

  	sort(A,A+N);
  	result=A[0];
  for(i=1;i<N;i++)
  {
  	result=(result+A[i])/2;
  }
  cout<<result<<endl;
  }
  return 0;
}

Measured memory_bytes: 256
fun main() {
    val (n, m, x) = readLine()!!.split(" ").map(String::toInt)
    val list = Array(n) {
        readLine()!!.split(" ").map(String::toInt).toIntArray()
    }


    var min = Integer.MAX_VALUE
    for (bit in 0 until (1 shl n)) {
        val result = IntArray(m + 1)
        for(i in 0 until n){
            if(1 and (bit shr i) == 0){
                repeat(m + 1) { idx ->
                    result[idx] = result[idx] + list[i][idx]
                }
            }
        }
        if (result.drop(1).all { it >= x }) {
            min = Math.min(result[0], min)
        }
    }

    if (min == Integer.MAX_VALUE) {
        println(-1)
    } else {
        println(min)
    }
}





Measured memory_bytes: 43212
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const ll INF = 1e16;
const ll mod = 1000000007;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)

template<class T> inline bool chmax(T& a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template<class T> inline bool chmin(T& a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
int ctoi(char c) {
  if (c >= '0' && c <= '9') {
    return c - '0';
  }
  return 0;
}
ll factorial(ll n) {
  if (n == 1) {
    return 1;
  }
  ll ans = factorial(n-1);
  return ans*n;
}
ll gcd(ll a, ll b) {
  if (b == 0) return a;
  return gcd(b, a%b);
}
ll lcm(ll a, ll b) {
  return (a/gcd(a, b))*b;
}
bool is_prime(ll n) {
  for (ll i = 2; i*i <= n; i++) {
    if (n%i == 0) return false;
  }
  return n != 1;
}
ll mod_pow(ll x, ll n, ll mod) {
  if (n == 0) return 1;
  ll res = mod_pow(x*x%mod, n/2, mod);
  if (n & 1) res = res * x % mod;
  return res;
}

int main() {
  ll n, m;
  cin >> n >> m;
  vector <ll> v(n);
  rep(i, n) cin >> v.at(i);
  vector <ll> tmp(n, 1);
  rep(i, m) {
    ll a, b;
    cin >> a >> b;
    a--;
    b--;
    if (v.at(a) > v.at(b)) {
      tmp.at(b) = 0;
    } else {
      tmp.at(a) = 0;
      if (v.at(a) == v.at(b)) tmp.at(b) = 0;
    }
  }
  ll res = 0;
  rep(i, n) res += tmp.at(i);
  cout << res << endl;
}

Measured memory_bytes: 4848
import sys
from itertools import combinations
def input(): return sys.stdin.readline().strip()


def main():
    """
    https://www.quora.com/How-can-I-find-the-maximum-Manhattan-distance-between-2-points-from-a-given-set-of-points
    """
    N = int(input())
    x, y = map(int, input().split())
    maximalp = x + y
    minimalp = x + y
    maximaln = x - y
    minimaln = x - y
    for _ in range(N - 1):
        x, y = map(int, input().split())
        maximalp = max(maximalp, x + y)
        minimalp = min(minimalp, x + y)
        maximaln = max(maximaln, x - y)
        minimaln = min(minimaln, x - y)
    print(max(maximalp - minimalp, maximaln - minimaln))


if __name__ == "__main__":
    main()


Measured memory_bytes: 68544
#include <bits/stdc++.h>
 
using namespace std;
 
#define rep(i,n) for(int i = 0 ; i < n ; i++)
 
int H,W,N,x,y,d;
char f[20][20],c;
char dir[]={'U','D','L','R'};
char m[]={'^','v','<','>'};
int dx[]={0,0,-1,1};
int dy[]={-1,1,0,0};
bool flg;
 
bool inField(int y,int x){
    return 0 <= y && y < H && 0 <= x && x < W;
}
 
void shoot(){
    int tx = x, ty = y;
    while(true){
	int nx = tx + dx[d],ny = ty + dy[d];
	if(!inField(ny,nx) || f[ny][nx] == '#') return;
	if(f[ny][nx] == '*'){ f[ny][nx] = '.'; return; }
	tx = nx, ty = ny;
    }
}
 
void move(){
    int nx = x + dx[d],ny = y + dy[d];
    f[y][x] = m[d];
    if(!inField(ny,nx) || f[ny][nx] == '-' ||
       f[ny][nx] == '#' || f[ny][nx] == '*') return;
    f[y][x] = '.'; f[ny][nx] = m[d];
    y = ny, x = nx;
}
 
void print(){
    rep(i,H){
	rep(j,W){
	    cout << f[i][j];
	}
	cout << endl;
    }
}
 
void solve(){
    cin >> N;
    rep(i,N){
    
	cin >> c;
	if(c == 'S'){
	    rep(j,4){
		if(m[j] == f[y][x]){
		    d = j;
		    break;
		}
	    }
	    shoot();
	}else{
	    rep(j,4){
		if(dir[j] == c){
		    d = j;
		    break;
		}
	    }
	    move();
	}
    }
    print();
}
 
int main(){
    int Tc;
    cin >> Tc;
    while(Tc--){
	if(flg) cout << endl; flg = true;
	cin >> H >> W;
	rep(i,H) rep(j,W){
	    cin >> f[i][j];
	    if(f[i][j] == '<' || f[i][j] == '>' ||
	       f[i][j] == '^' || f[i][j] == 'v'){
		y = i; x = j;
	    }
	}
	solve();
    }
    return 0;
}

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

using namespace std;

#define _ ios_base::sync_with_stdio(0);cin.tie(0);
#define endl '\n'
#define f first
#define s second

typedef long long ll;

const int INF = 0x3f3f3f3f;
const ll LINF = 0x3f3f3f3f3f3f3f3fll;

const int MOD = 1e9+7, MAX = 2e5+10;

ll inv(ll a, ll b){
	if(1 < a) return b - inv(b % a, a) * b/a;
	else return 1;
}

ll fat[MAX], finv[MAX];

void build() {
	fat[0] = 1;
	finv[0] = inv(0,MOD);
	for(ll i=1; i<MAX; i++) {
		fat[i] = fat[i-1]*i % MOD;
		finv[i] = inv(fat[i],MOD);
	}
}

ll C(ll n, ll m) {
	if(m > n) return 0;
	return fat[n] * finv[m] % MOD * finv[n-m] % MOD;
}

int main(){ _

	int h, w, n; cin >> h >> w >> n;

	vector<vector<pair<int,int>>> sqr(h);
	for(int i=0; i<n; i++) {
		int r, c; cin >> r >> c; r--, c--;
		sqr[r].push_back({c,i});
	}

	build();

	for(int i=0; i<h; i++)
		sort(sqr[i].begin(), sqr[i].end());

	vector<ll> memo(n,0); // in/out, which sqr
	vector<vector<pair<int,int>>> vis(w); // line, idx (put in col)

	auto ways = [&] (int i1, int j1, int i2, int j2) {
		int W = i2-i1+1;
		int H = j2-j1+1;
		return C(W+H-2,H-1);
	};

	ll ans = ways(0,0,h-1,w-1);
	for(int i=0; i<h; i++) for(auto look : sqr[i]) { // col, idx
		
		ll now = ways(0,0,i,look.f);
		for(int j=0; j <= look.f; j++) {
			for(auto k : vis[j]) {
				now = (now - ways(k.f,j,i,look.f)*memo[k.s]) % MOD;
			}
		}
		now = (now + MOD) % MOD;

		memo[look.s] = now;

		vis[look.f].push_back({i,look.s});
		ans = (ans - ways(i,look.f,h-1,w-1)*memo[look.s]) % MOD;
	}

	cout << (ans+MOD) % MOD << endl;

	return 0;
}


Measured memory_bytes: 11380
import static java.lang.System.out;
import static java.util.Arrays.asList;
import java.io.File;
import java.util.Scanner;

public class Main extends BaseExe {

    public static void main(String[] args) {
        new Main().exeSysIn();
    }

    @Override
    protected void execute() throws Exception {
        String data;
        while (!"0 0".equals(data = nextLine())) {
            StringBuffer sb = new StringBuffer();
            asList(data.split(" ")).stream().map(Integer::parseInt).sorted()
                    .forEach(num -> sb.append(num + " "));
            out.println(sb.substring(0, sb.length() - 1).toString());
        }
    }
}

abstract class BaseExe {

    private Scanner scan;

    private String inData;

    public void exeSysIn() {
        try (Scanner scan = new Scanner(System.in)) {
            exeCtrl(scan);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void exeFileIn(String inDataPath) {
        try (Scanner scan = new Scanner(new File(inDataPath))) {
            exeCtrl(scan);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void exeCtrl(Scanner scan) throws Exception {
        this.scan = scan;
        execute();
    }

    protected abstract void execute() throws Exception;

    protected String nextLine() {
        return scan.nextLine();
    }

    protected boolean judgeInData() {
        return hasNextLine() && isNotEmptyInData();
    }

    protected boolean hasNextLine() {
        return scan.hasNextLine();
    }

    protected boolean isNotEmptyInData() {
        inData = nextLine();
        return !inData.isEmpty();
    }

    protected String getInData() throws Exception {
        if (inData.isEmpty())
            throw new Exception("inData is Empty.");
        return inData;
    }
}

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

class segtree{
    int *tree;
    size_t len, depth;
public:
    segtree(size_t n_){
        for(len=depth=1;len<n_;len<<=1) ++depth;
        tree = new int[2*len];
        for(size_t i=0;i<2*len;++i) tree[i] = 0;
    }

    void add(size_t left, size_t right, int val){
        left += len;
        right += len;
        for(size_t d=0;d<depth;++d){
            if(right <= left) return;
            if(left + 1 == right){
                tree[left] += val;
                return;
            }
            if(left & 1){
                tree[left] += val;
                ++left;
            }
            left >>= 1;
            if(right & 1) tree[right-1] += val;
            right >>= 1;
        }
    }

    void add(size_t pos, int val){
        tree[len+pos] += val;
    }

    int get(size_t pos){
        pos += len;
        for(size_t d=depth-1;d>0;--d){
            size_t p = pos >> d;
            tree[2*p+0] += tree[p];
            tree[2*p+1] += tree[p];
            tree[p] = 0;
        }
        return tree[pos];
    }
};

int main(){
    int n, m;
    vector<pair<int,pair<int,int>>> area;

    cin >> n >> m;
    segtree st(m+1);

    for(int i=0;i<n;++i){
        int l, r;
        cin >> l >> r;
        area.push_back({r-l+1, {l, r}});
    }
    sort(area.begin(), area.end());

    int p = 0;
    for(int d=1;d<=m;++d){
        for(;p<n && area[p].first<d;++p){
            st.add(area[p].second.first, area[p].second.second+1, 1);
        }
        int ans = n - p;
        for(int pos=d;pos<=m;pos+=d) ans += st.get(pos);
        cout << ans << endl;
    }
    
    return 0;
}


Measured memory_bytes: 9456
/*     Abhinav Vaishya (abhinav_2812)          */
/*     DA-IICT                    */


#include<cstdio>
#include<iostream>
#include<string>
#include<vector>
#include<queue>
#include<stack>
#include<map>
#include<set>
#include<algorithm>
#include<cmath>
#include<cstring>
#include<cstdlib>
#include<iomanip>

using namespace std;

typedef long long LL;
typedef long double LD;
typedef vector<int> vi;
typedef vector<long long> vl;


#define MOD 1000000007
#define si(n) scanf("%d",&n)
#define sd(n) scanf("%lf",&n)
#define sl(n) scanf("%lld",&n)
#define sld(n) scanf("%Lf",&n)
#define ss(s) scanf("%s",s)
#define pi(n) printf("%d",n)
#define pd(n) printf("%lf",n)
#define pl(n) printf("%lld",n)
#define pld(n) printf("%Lf",n)
#define ps(s) printf("%s",s)
#define pline printf("\n")
#define _for(i,a,n) for(int i=a; i<n; i++)
#define __for(i,a,n) for(int i=a; i<=n; i++)
#define PB push_back
#define MP make_pair
#define F first
#define S second
#define ALL(x) x.begin(),x.end()

int max_ar(vi &a);
int min_ar(vi &a);
bool sortbysec(const pair<int,int> &a,const pair<int,int> &b);

//----Code starts here----//
int main() {
        int n;
        si(n);
        pi(n*800 - (n/15)*200); pline;
        return 0;
}
//----Code ends here----//



//maximum element in a vector
int max_ar(vi &a) {
        int mx=-1;
        _for(i,0,a.size()) mx=max(mx,a[i]);
        return mx;
}

//minimum element in a vector
int min_ar(vi &a) {
        int mn=1000000000;
        _for(i,0,a.size()) mn=min(mn,a[i]);
        return mn;
}

//sort a pair vector by second element
bool sortbysec(const pair<int,int> &a,const pair<int,int> &b) {
        return (a.second < b.second);
}


Measured memory_bytes: 256
#include<stdio.h>
int main(){
    int n,ax,ay,bx,by,cx,cy,dx,dy;
    double x,y,t;
    scanf("%d",&n);
    while(n--){
        scanf("%d%d%d%d%d%d%d%d",&ax,&ay,&bx,&by,&cx,&cy,&dx,&dy);
        t=(double)((cx-ax)*(dy-cy)-(cy-ay)*(dx-cx))/(double)((bx-ax)*(dy-cy)-(by-ay)*(dx-cx));
        x=ax+t*(bx-ax);
        y=ay+t*(by-ay);
        printf("%.10f %.10f\n",x,y);
    }
    return 0;
}

Measured memory_bytes: 640
#include<bits/stdc++.h>
#define N 405
using namespace std;
typedef pair<int,int> P;
vector<P> e[N];
int n,m,a,b,c;
int memo[N];

int dfs(int x,int prevc){
  if(x==n-1)return prevc;
  if(memo[x])return memo[x]+prevc;
  for(int i=0;i<e[x].size();i++){
    int nx=e[x][i].first;
    memo[x]=max(memo[x],dfs(nx,e[x][i].second));
  }
  return memo[x]+prevc;
}

int main(){
  cin>>n>>m;
  for(int i=0;i<m;i++){
    cin>>a>>b>>c;
    e[a].push_back(P(b,c));
  }
  cout<<dfs(0,0)<<endl;
  return 0;
}

Measured memory_bytes: 1216
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
using ll = long long;
int main() {
  int n, k;
  cin >> n >> k;
  vector<int> a(n);
  for(int i = 0; i < n; i++) {
    cin >> a[i];
  }
  sort(a.begin(), a.end());
  int sum = 0;
  for(int i = 0; i < k; i++) {
    sum += a[i];
  }
  cout << sum << endl;
  return 0;
}

Measured memory_bytes: 3632
while True :
  n, x = map(int, input().split())
  if(n == 0 and x == 0) :
    break
  else :
    s = 0
    for i in range(1, n + 1) :
      for j in range(1, i) :
        for k in range(1, j) :
          if(i + j + k == x) :
            s += 1
          else :
            pass
    print(s)


Measured memory_bytes: 5596
import java.util.*;

public class Main {

  public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    String s1 = sc.next();
    String s2 = sc.next();
    String s3 = sc.next();
    String n1 = s1.substring(0,1);
    String n2 = s2.substring(0,1);
    String n3 = s3.substring(0,1);
    String a = n1.toUpperCase();
    String b = n2.toUpperCase();
    String c = n3.toUpperCase();

    System.out.println(a+b+c);

  }

}


Measured memory_bytes: 21844
// Winners don't quit and Quitters don't win
#include<bits/stdc++.h>
using namespace std;
#define ll long long int
#define pb push_back
#define pii pair<int,int>
#define pll pair<ll,ll>
#define vii vector<int>
#define vll vector<ll>
#define mod 998244353
#define MAX 100005
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    ll i,j,n,m,k,s=0,c=0,x,diff,ans=0;
    cin>>n>>m>>k;
    vll a,b;
    vll prefix1(n+1,0);
    vll prefix2(m+1,0);
    for(i=0;i<n;++i){
    	cin>>x;
    	a.pb(x);
	}
	for(i=0;i<m;++i){
		cin>>x;
		b.pb(x);
	}
	prefix1[1] = a[0];
	prefix2[1] = b[0];
	for(i=2;i<=n;++i){
		prefix1[i] += prefix1[i-1] + a[i-1];
	}
	for(i=2;i<=m;++i){
		prefix2[i] += prefix2[i-1] + b[i-1];
	}
	for(i=0;i<=n;++i){
		diff = k - prefix1[i];
		x = upper_bound(prefix2.begin(),prefix2.end(),diff) - prefix2.begin();
	   if(diff>=0)
		ans = max(ans,x+i-1);
		//cout<<x+i<<" ";
	}
	cout<<ans;
}

Measured memory_bytes: 11068
#include <iostream>
#include<sstream>
#include<string>
#include<vector>
#include <cmath>
#include <limits>
#include <queue>
#include<algorithm>
#include<cstring>
#include <cstdlib>
#include <cstdio>
#include <set>
#include <iomanip>
#include <map>
#include <stack>
#include <memory>
 
using namespace std;
 
#define rep(i,n) for (int (i)=0;(i)<(n);i++)
const long long MAX = 1e9;


int main(){
    long long n;
    cin >> n;
    vector<long long> v;
    v.push_back(3); v.push_back(5); v.push_back(7);
    vector<long long> tempv = v;
    rep(i,9){
        vector<long long> tempv2;
        rep(j,tempv.size()){
            tempv2.push_back(tempv[j]+pow(10,i+1)*3);
            tempv2.push_back(tempv[j]+pow(10,i+1)*5);
            tempv2.push_back(tempv[j]+pow(10,i+1)*7);
        }
        v.insert(v.end(), tempv2.begin(),tempv2.end());
        tempv = tempv2;
    }
    vector<long long> v753;
    rep(i,v.size()){
        string s = to_string(v[i]);
        map<int, int> m;
        rep(j,s.length()){
            m[s[j]-'0'] = 1;
        }
        if (m.size() == 3){
            v753.push_back(v[i]);
        }
    }
    long long res = 0;
    rep(i,v753.size()){
        if (v753[i] <= n) res++;
    }
    cout << res << endl;
    return 0;
}


Measured memory_bytes: 2948
from collections import Counter
import sys
s = input()
n = len(s)
c = Counter(s)
most_times = 0
for value, times in c.items():
    most_times = max(most_times, times)
if most_times == 1:
    print('YES')
    sys.exit()
can = True
if n%3 == 0 and n//3 < most_times:
    can = False
if n%3 != 0 and n//3 + 1 < most_times:
    can = False
if can:
    print('YES')
else:
    print('NO')



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

using namespace std;

string solve(const string& S,const string& T)
{
	auto N = S.size();

	map<char, char> s_table, t_table;

	for(size_t i=0; i<N; i++)
	{
		auto c = S[i];
		auto d = T[i];

		// procuro c em s_table
		auto it = s_table.find(c);

		// se não achar
		if(it == s_table.end())
		{
			// procuro em t_table
			auto jt = t_table.find(d);

			// se não achar
			if(jt == t_table.end())
			{
				s_table[c] = d;
				t_table[d] = c;
			}

			else
				return "No";
		}

		else if (it->second != d)
			return "No";
	}

	return "Yes";

}

int main()
{
	ios::sync_with_stdio(false);

	string n, m;
	cin >> n >> m;

	string ans = solve(n, m);

	cout << ans << endl;
}

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

int main(){
Int n;
Int a[300010];
        cin >> n;
        for(Int i = 0; i < 3*n; i++) cin >> a[i];
        sort(a,a+3*n);
 
        Int ans = 0;
        int cnt = 0;
        for(Int i = 3*n-2; i >= 0; i -= 2){
                if(cnt == n) break;
                ans += a[i];
                cnt++;
        }
 
        cout << ans << endl;
        return 0;
}

Measured memory_bytes: 2560
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, n) for (int i = 0; i < (int)n; ++i)
#define repi(i, a, b) for (int i = int(a); i < int(b); ++i)
#define repr(i, n) for (int i = (int)n - 1; i >= 0; --i)
#define ALL(a) (a).begin(), (a).end()
const ll mod = 1e9 + 7;

int main() {
  int n, m;
  cin >> n >> m;
  vector<int> x(m);
  rep(i, m) cin >> x[i];
  sort(ALL(x));
  vector<int> d(m - 1);
  rep(i, m - 1) d[i] = x[i + 1] - x[i];
  sort(ALL(d));
  int ans = x[m - 1] - x[0];
  if (n >= m)
    cout << 0 << endl;
  else {
    rep(i, n - 1) ans -= d[m - 2 - i];
    cout << ans << endl;
  }
  return 0;
}

Measured memory_bytes: 1024
#include <iostream>

using namespace std;

int main()
{
  	int a, b, c;
  	cin >> a >> b;
  	if(b == 100){
       c = a * 1000 + b;
    }else if(b < 10){
	   c = a * 	10 + b;
    }else{
       c = a * 100 + b;
    }
  	bool t = false;
  	for(int i = 1; i < 320; i++){
      	if(i * i == c){
          	t = true;
          	break;
        }
    }
  	if(t){
      	cout << "Yes";
    }else{
      	cout << "No";
    }
  	return 0;
}


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

int main() {
	long long h,w;
	long long ans;

	cin >> h >> w;

	if(h%2==0){
		ans=(h/2)*w;
	}
	else if(h%2==1 && w%2==0){
		ans=((h-1)/2)*w+w/2;
	}
	else if(h%2==1 && w%2==1){
		ans=((h-1)/2)*w+(w-1)/2+1;
	}

	if(h==1 || w==1){
		ans = 1;
	}

	cout << ans << endl;
}

Measured memory_bytes: 256
#include <bits/stdc++.h>
using namespace std;
//NOT TO SUBMIT IN WRONG PLACE OR BY WRONG COMPILER
//ABC 167, task B, 2020/05/10
//ms,
/*
*/

int main(void){
  int64_t K, posi, zero, nega;
  int64_t ans=0;

  cin >>posi >>zero >>nega >>K;

  if(K<=posi) ans=K;
  else if(K-posi<=zero) ans=posi;
  else ans = posi - (K-posi-zero);

  cout <<ans <<endl;

  return 0;
}
/*
*/


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

int main(void){
   int n;
   cin >> n;
   vector<int> a(n);
   vector<int> b(n - 1);
   for(int i = 0;i < n - 1;i++){
         cin >> b[i];
   }
   
   a[0] = a[1] = b[0];
   
   for(int i = 1;i < n - 1;i++){
         a[i + 1] = b[i];
         if(a[i] > a[i + 1])a[i] = a[i + 1];
   }
   int ans = 0;
   for(int i = 0;i < n;i++){
        ans += a[i]; 
   }
   cout << ans << endl;
}


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

int main(){
    int n;
    cin >> n;
    vector<pair<int,int>> a(n,pair<int,int>(0,0));

    for(int j=0;j<n;j++){
        cin >> a[j].first;
        if(j!=0){
            a[j].first = a[j-1].first + a[j].first;
        }
    }
    for(int j = 0;j<n;j++){
        cin >> a[j].second;
        if(j!=0){
            a[j].second = max(a[j].first,a[j-1].second) + a[j].second;
        }
        else{
            a[j].second = a[j].second + a[j].first;
        }
    }
    cout << a[n-1].second << endl;
    return 0;
}

Measured memory_bytes: 256
n = int(input())
p = list(map(int,input().split()))
count = 0
for i in range(1, n-1):
    if p[i - 1] < p[i] < p[i + 1]:
        count += 1
    elif p[i] < p[i - 1] == p[i + 1]:
        count += 1
    elif p[i - 1] > p[i] > p[i + 1]:
        count += 1
print(count)



Measured memory_bytes: 9132
n=int(input())
s=[]
d=[]
i=0
while i!=n:
    s=list(map(int,input().split()))
    d.append(s)
    i+=1
#print(d)
i=0
f=[]
s=[]
j=0
while i!=n:
    while j!=n:
        if i!=j:
            f.append(d[i][0]-d[j][0])
            f.append(d[i][1] - d[j][1])
            s.append(f)
            f=[]

        j += 1
    i+=1
    j=0


#print(s)
i=0
f=[]
while i!=len(s):
    f.append(s.count(s[i]))
    i+=1
#print(f)
try:
    print(int(n-max(f)))
except:
    print(1)

Measured memory_bytes: 3444
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
#include <limits.h>
#include <stdbool.h>

#define rep(i, n) for(int i=0; i<(n); ++i)
#define max  200001

//qsort(str, n, sizeof(int), up_comp);
int up_comp(const void *a, const void *b){return *(int*)a - *(int*)b;}
int down_comp(const void *a, const void *b){return *(int*)b - *(int*)a;}
int int_sort( const void * a , const void * b ) {return strcmp(( char * )a , ( char * )b );}
int count_digit(int a){int i=0;while(a!=0){a=a/10;i++;}return i;}
int euclid( int a, int b ){int temp;if(a<b){temp=a;a=b;b=temp;}if(b<1)return -1;if(a%b==0) return b;return euclid(b,a%b);}

int main(void){
    
    int n, a[max];
    int cnt=0, flag=0;
    
    scanf("%d", &n);
    rep(i, n){
        scanf("%d", &a[i]);
    }
    
    while (1) {
        rep(i, n){
            if (a[i]%2==0) {
                a[i]/=2;
            } else {
                flag++;
            }
        }
        if (flag) {
            break;
        } else {
            cnt++;
        }
    }
    
    printf("%d", cnt);

    return 0;
}


Measured memory_bytes: 1704
#ifdef __GNUC__
#pragma GCC diagnostic ignored "-Wunused-result"
#pragma GCC target("avx2")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#endif

#include <algorithm>
#include <cmath>
#include <cstring>
#include <iostream>
#include <sstream>
#include <numeric>
#include <map>
#include <set>
#include <queue>
#include <vector>

using namespace std;

typedef long long LL;

static const long long MOD = 1000000007;

struct modll {
	long long x;
	modll() { }
	modll(int _x) : x(_x) { }
	operator int() const { return (int)x; }
	modll operator+(int y) { return (x + y + MOD) % MOD; }
	modll operator+=(int y) { x = (x + y + MOD) % MOD; return *this; }
	modll operator-(int y) { return (x - y + MOD) % MOD; }
	modll operator-=(int y) { x = (x - y + MOD) % MOD; return *this; }
	modll operator*(int y) { return (x * y) % MOD; }
	modll operator*=(int y) { x = (x * y) % MOD; return *this; }
	modll operator/(int y) { return (x * modpow(y, MOD - 2)) % MOD; }
	modll operator/=(int y) { x = (x * modpow(y, MOD - 2)) % MOD; return *this; }
	static modll modinv(int a) { return modpow(a, MOD - 2); }
	static modll modpow(int a, int b) {
		modll x = a, r = 1;
		for (; b; b >>= 1, x *= x) if (b & 1) r *= x;
		return r;
	}
};

void solve(long long N, std::vector<int> &A) {
	modll ans = 0, sum = 0;
	for (LL i = 0; i < N; ++i) {
		ans += sum * A[i];
		sum += A[i];
	}
	cout << ans << endl;
}

int main() {
    long long N;
	scanf("%lld", &N);
	std::vector<int> A(N);
	for (int i = 0; i < N; i++) {
		scanf("%d", &A[i]);
	}
	solve(N, A);
	return 0;
}


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

#define FI(i,a,b) for(int i=(a);i<=(b);i++)
#define FD(i,a,b) for(int i=(a);i>=(b);i--)

#define LL long long
#define Ldouble long double

using namespace std;

int cnt[26];

int h, w;

char s[105][105];
int one, two, four;

int main(){
  scanf("%d %d", &h, &w);
  FI(i, 1, h){
    scanf(" %s", s[i] + 1);
    FI(j, 1, w) cnt[s[i][j] - 'a']++;
  }
  if(h % 2 == 1 && w % 2 == 1) one++;
  if(h % 2 == 1) two += w / 2;
  if(w % 2 == 1) two += h / 2;
  four = (w / 2) * (h / 2);
  assert(one + 2 * two + 4 * four == h * w);
  FI(i, 0, 25){
    while(cnt[i]){
      if(cnt[i] >= 4 && four > 0){
        four--;
        cnt[i] -= 4;
        continue;
      }
      if(cnt[i] >= 2 && two > 0){
        two--;
        cnt[i] -= 2;
        continue;
      }
      if(cnt[i] > 0 && one > 0){
        one--;
        cnt[i]--;
        continue;
      }
      printf("No\n");
      return 0;
    }
  }
  printf("Yes\n");
  return 0;
}

Measured memory_bytes: 256
from sys import stdin
from collections import deque


class BreadthFirstSearch:
    def __init__(self, num_nodes):
        self.n = num_nodes
        self.adj_list = [[] for _ in range(self.n)]

    # Edge数分回すことを想定
    def add_edge(self, start, end, undirected=True):
        self.adj_list[start].append(end)
        if undirected:
            self.adj_list[end].append(start)

    # depth = distance
    def search(self, start_node):
        order = [-1] * self.n  # a bfs ordering of each vertex
        parent = [-1] * self.n  # parent of each vertex in the bfs search tree
        depth = [-1] * self.n  # the depth of each vertex
        q = deque([(start_node, -1, 0)])  # (vertex, parent, depth)
        num = 0  # current ordering
        while len(q) > 0:
            v, p, d = q.popleft()
            if order[v] < 0:  # visited v for the first time
                order[v] = num
                parent[v] = p
                depth[v] = d
                num += 1
                for u in self.adj_list[v]:
                    if order[u] >= 0:
                        continue
                    q.append((u, v, d+1))
        return order, parent, depth


def get_result(data):
    N, M = data[0]
    A = data[1:]
    solver = BreadthFirstSearch(N)
    for val in A:
        solver.add_edge(val[0]-1, val[1]-1)
    order, parent, depth = solver.search(0)
    print('Yes')
    for p in parent[1:]:
        print(p+1)
    return None


if __name__ == '__main__':
    raw_data = [val.rstrip() for val in stdin.readlines()]
    data = [list(map(int, val.split(' '))) for val in raw_data]
    result = get_result(data)
    # print(result)


Measured memory_bytes: 168020
from sys import stdin
from collections import deque

def read_graph(n, k):
    A = [ [] for _ in range(n) ]
    for _ in range(k):
        line = stdin.readline().strip().split()
        A[int(line[0])].append(int(line[1]))
    return A

def topological_sort(n, A):
    color = ["WHITE"] * n
    Q = deque([])
    out = []

    indeg = [0] * n
    for i in range(n):
        for j in A[i]:
            indeg[j] = indeg[j] + 1

    def bfs(s):
        nonlocal color
        nonlocal indeg
        nonlocal Q
        color[s] = "GRAY"
        Q.append(s)

        while Q:
            u = Q.popleft()
            out.append(u)

            for v in A[u]:
                indeg[v] = indeg[v] - 1
                if indeg[v] == 0 and color[v] == "WHITE":
                    color[v] = "GRAY"
                    Q.append(v)

    for i in range(n):
        if indeg[i] == 0 and color[i] == "WHITE":
            bfs(i)

    return out

n, k = [int(i) for i in input().split()]
A = read_graph(n, k)
B = topological_sort(n, A)
for i in B:
    print(i)



Measured memory_bytes: 9148
def main():
  n, k = [int(e) for e in input().split()]
  h = [int(e) for e in input().split()]
  cost = [0] * n
  for i in range(1, n):
    cost[i] = min(abs(h[i] - h[j]) + cost[j] for j in range(max(0, i - k), i))
  print(cost[n - 1])
main()


Measured memory_bytes: 13980
#include <bits/stdc++.h>
using namespace std;
int main(void)
{
    int N,X;
    cin >> N >> X;
    int A=0;
    vector<int> ks(N);
    int U=0;
    for (int i=0;i<N;i++){
        cin >> ks.at(i);
        A+=ks.at(i);
        
        U++;
    }

   X-=A;

   sort(ks.begin(),ks.end());

   int P;
   P=X/ks.at(0);
   cout << U+P <<endl;
}

Measured memory_bytes: 256
import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Scanner;

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

    static class ABC108B {
        public void solve(int testNumber, Scanner in, PrintWriter out) {
            int x1 = in.nextInt();
            int y1 = in.nextInt();
            int x2 = in.nextInt();
            int y2 = in.nextInt();

            int x3 = x2 - (y2 - y1);
            int y3 = y2 + (x2 - x1);
            int x4 = x3 - (x2 - x1);
            int y4 = y3 - (y2 - y1);

        /*
        if (y1 == y2 && x1 > x2) {
            x3 = x2;
            y3 = y2 - (x1 - x2);
            x4 = x1;
            y4 = y2 - (x1 - x2);
        } else if (x1 == x2 && y2 < y1) {
            x3 = x2 + (y1 - y2);
            y3 = y2;
            x4 = x2 + (y1 - y2);
            y4 = y1;
        } else if (y1 == y2 && x1 < x2) {
            x3 = x2;
            y3 = x1 + (x2 - x1);
            x4 = x1;
            y4 = x1 + (x2 - x1);
        } else if (x1 == x2 && y1 < y2) {
            x3 = x2 - (y2 - y1);
            y3 = y2;
            x4 = x2 - (y2 - y1);
            y4 = y1;
        }
        */

            out.println(x3 + " " + y3 + " " + x4 + " " + y4);
        }

    }
}



Measured memory_bytes: 21844
#include <bits/stdc++.h>
using namespace std;
#define INF 1<<30
#define endl '\n'
#define maxn 100005
#define tc printf("Case %d: ", cs)
#define tcn printf("Case %d:\n", cs);
#define FASTIO ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);
typedef long long ll;
const double PI = acos(-1.0);

#define dbg1(x) cerr << #x << " = " << x << endl;
#define dbg2(x, y) cerr << #x << " = " << x << ", " << #y << " = " << y << endl;
#define dbg3(x, y, z) cerr << #x << " = " << x << ", " << #y << " = " << y << ", " << #z << " = " << z << endl;
#define dbg4(w,x, y, z) cerr << #w << " = " << w << ", " <<#x << " = " << x << ", " << #y << " = " << y << ", " << #z << " = " << z << endl;

template < typename F, typename S >
ostream& operator << ( ostream& os, const pair< F, S > & p ) {
  return os << "(" << p.first << ", " << p.second << ")";
}

template < typename T >
ostream &operator << ( ostream & os, const vector< T > &v ) {
  os << "{";
  for (auto it = v.begin(); it != v.end(); ++it) {
    if ( it != v.begin() ) os << ", ";
    os << *it;
  }
  return os << "}";
}

template < typename T >
ostream &operator << ( ostream & os, const set< T > &v ) {
  os << "[";
  for (auto it = v.begin(); it != v.end(); ++it) {
    if ( it != v.begin()) os << ", ";
    os << *it;
  }
  return os << "]";
}

template < typename F, typename S >
ostream &operator << ( ostream & os, const map< F, S > &v ) {
  os << "[";
  for (auto it = v.begin(); it != v.end(); ++it) {
    if ( it != v.begin() ) os << ", ";
    os << it -> first << " = " << it -> second ;
  }
  return os << "]";
}

#define dbg(args...) do {cerr << #args << " : "; faltu(args); } while(0)

clock_t tStart = clock();
#define timeStamp dbg("Execution Time: ", (double)(clock() - tStart)/CLOCKS_PER_SEC)

void faltu () { cerr << endl; }

template <typename T>
void faltu( T a[], int n ) {
  for (int i = 0; i < n; ++i) cerr << a[i] << ' ';
  cerr << endl;
}

template <typename T, typename ... hello>
void faltu( T arg, const hello &... rest) { cerr << arg << ' '; faltu(rest...); }
// Program showing a policy-based data structure.
#include <ext/pb_ds/assoc_container.hpp> // Common file 
#include <ext/pb_ds/tree_policy.hpp>
#include <functional> // for less 
#include <iostream>
using namespace __gnu_pbds;
using namespace std;
// GNU link : https://goo.gl/WVDL6g
typedef tree<int, null_type, less_equal<int>, rb_tree_tag,
        tree_order_statistics_node_update>
        new_data_set;

/**___________________________________________________**/



int main()
{
  FASTIO
  /*
#ifndef ONLINE_JUDGE
  freopen("in.txt", "r", stdin);
  freopen("out.txt", "w", stdout);
  freopen("error.txt", "w", stderr);
#endif
//*/
  int T;
  //scanf("%d", &T);
  T = 1;
  for (int cs = 1; cs <= T; cs++) {
    int n;
    cin >> n;
    string s;
    cin >> s;
    map<int, char> ans;
    int i = 0;
    for (char ch = 'A'; ch <= 'Z'; ch++) ans[i++] = ch;
    for (auto ch : s) {
      int x = ch - 'A' + n;
      cout << ans[x % 26];
    }
  }
  return 0;
}

Measured memory_bytes: 256