akhauriyash/Code-Regression huggingface dataset:
S = input()
Q = int(input())
queries = []
for _ in range(Q):
    queries.append(input())

# for query in queries:
#     if query == "1":
#         S = S[::-1]
#     else:
#         _, F, C = list(query.split())
#         if F == "1":
#             S = C + S
#         else:
#             S = S + C
# print(S)

left = ""
right = ""
target = 0

for query in queries:
    if query == "1":
        target = 1 - target
    else:
        _, F, C = list(query.split())
        if F == "1":
            if target == 0:
                left += C
            else:
                right += C
        else:
            if target == 0:
                right += C
            else:
                left += C
if target == 0:
    print("{}{}{}".format(left[::-1], S, right))
else:
    print("{}{}{}".format(right[::-1], S[::-1], left))


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


#define INF 1000000000000


signed main()
 {
string s,t;
cin>>s>>t;
vector<char>a(s.size());
vector<char>b(t.size());
rep(i,s.size()){
  a[i]=s[i];
}
rep(i,t.size()){
  b[i]=t[i];
}
sort(a.begin(),a.end());
sort(b.begin(),b.end());
reverse(b.begin(),b.end());

rep(i,s.size()){
  s[i]=a[i];
}
rep(i,t.size()){
  t[i]=b[i];
}
if(s<t) cout<<"Yes";
else cout<<"No";

}


Measured memory_bytes: 256
#include <stdio.h>

int gcd(int x, int y) { return !y ? x : gcd(y, x % y); }

int num[222][222];

int main(void)
{
	int n, i, j, k, ans = 0;

	scanf("%d", &n);

	for (i = 1; i <= n; i++)
		for (j = 1; j <= n; j++)
			num[i][j] = gcd(i, j);

	for (i = 1; i <= n; i++)
		for (j = 1; j <= n; j++)
			for (k = 1; k <= n; k++)
				ans += num[num[i][j]][k];

	printf("%d", ans);
	return 0;
}

Measured memory_bytes: 1900
#include <bits/stdc++.h>
#define _GLIBCXX_DEBUG
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
typedef long long ll;
const ll MOD = 1e9+7;

/*bool empty_pq(priority_queue<int> &R){
  if(R.size()==0) return true;
  else return false;
}*/

int main(){
  int n;
  int64_t ab;
  cin >> n;
  vector<int64_t> A(n), B(n);
  rep(i, n) cin >> A[i];
  rep(i, n) cin >> B[i];
  priority_queue<int> R;
  //int rest=0;
  int64_t count = 0, ans=0, sum=0;
  rep(i, n){
    if(B[i]<A[i]){
      ab = A[i]-B[i];
      R.push(ab);
      count++;
    }
    else if(A[i]==B[i])  count++;
    else {
      ans++;
      sum += B[i]-A[i];
    }
  }
  //cout << count << endl;
  if(count==n) cout << 0 << endl; 
  else{
  int64_t q;  
  /*q = R.top();
  R.pop();
  ans++;*/  
  rep(i, n){
  q = R.top();
  R.pop();
  ans++;  
  sum -= q;
  if(sum<=0) break;
  else if(R.size()==0){
    ans = -1;
    break;
  } 
  }  
   cout << ans << endl; 
  }
}

Measured memory_bytes: 2176
#include <bits/stdc++.h>
#define rep(i,n) for(int i=0; i<(n); i++)
using ll = long long;
#define fast_io(); std::ios_base::sync_with_stdio(0); std::cin.tie(0); std::cout.tie(0);
#define rad2deg(a) ((a)/M_PI * 180.0)
#define deg2rad(a) ((a)/180.0 * M_PI)
const std::string yes = "Yes";
const std::string no  = "No";


int main(){
    int n,d;
    std::cin >> n >> d;

    int ans=0;
    double x,y,buf;
    for(int i=0; i<n; i++){
        std::cin >> x >> y;
        buf = sqrt(x*x+y*y);
        if(d>=buf) ans++;
    }

    std::cout << ans << "\n";
    return 0;
}

Measured memory_bytes: 3844
#include <bits/stdc++.h>
#include <boost/multiprecision/cpp_int.hpp>
using namespace std;

//#define int long long
using bll = boost::multiprecision::cpp_int;
using ll = long long;
//constexpr int INF = 1e9;//INT_MAX=(1<<31)-1=2147483647
constexpr ll INF = (ll)1e18;//(1LL<<63)-1=9223372036854775807
constexpr ll MOD = (ll)1e9 + 7;
constexpr double EPS = 1e-9;
constexpr int dx[4]={1,0,-1,0};
constexpr int dy[4]={0,1,0,-1};
#ifdef LOCAL_ENV
	#define debug(var) std::cout<<#var" = "<<var<<std::endl
#else
	#define debug(var)
#endif
#define p(var) std::cout<<var<<std::endl
#define rep(i, n)   for(ll i=0, i##_length=(n); i< i##_length; ++i)
#define repeq(i, n) for(ll i=1, i##_length=(n); i<=i##_length; ++i)
#define all(a) (a).begin(), (a).end()
#define PI (acos(-1))
template<typename T> inline T power(T x, T n){ return n ? power(x*x%MOD,n/2)*(n%2?x:1)%MOD : 1; }
template<typename T> inline T gcd(T a, T b) { return b ? gcd(b,a%b) : a; }
template<typename T> inline T lcm(T a, T b) { return a / gcd(a,  b) * b; }
template<typename T1, typename T2>inline bool chmax(T1 &a, T2 b) { return a < b && (a = b, true); }
template<typename T1, typename T2>inline bool chmin(T1 &a, T2 b) { return a > b && (a = b, true); }
template<typename T1, typename T2> ostream& operator<<(ostream& s, const pair<T1, T2>& p) {return s << "(" << p.first << ", " << p.second << ")";}
template<typename T> ostream& operator<<(ostream& s, const vector<T>& v) {
	for (int i = 0, len = v.size(); i < len; ++i){
		s << v[i]; if (i < len - 1) s << "\t";
	}
	return s;
}
template<typename T> ostream& operator<<(ostream& s, const vector< vector<T> >& vv) {
	for (int i = 0, len = vv.size(); i < len; ++i){
		s << vv[i] << endl;
	}
	return s;
}
template<typename T1, typename T2> ostream& operator<<(ostream& s, const map<T1, T2>& m) {
	s << "{" << endl;
	for (typeof(m.begin()) itr = m.begin(); itr != m.end(); ++itr){
		s << "\t" << (*itr).first << " : " << (*itr).second << endl;
	}
	s << "}" << endl;
	return s;
}

/*-----8<-----8<-----*/

constexpr ll N_MAX = 100000;

ll N;

void input() {
	scanf("%lld", &N);
}

ll f(string s){
	ll t=0;
	rep(i,s.size()){
		t+=s[i]-'0';
	}
	return t;
}

void solve() {
	ll ans = INF;

	repeq(i,N-1){
		ll j=N-i;
		ll t=f(to_string(i));
		t+=f(to_string(j));
		chmin(ans,t);
	}

	printf("%lld\n", ans);
}

signed main() {
	input();
	solve();
	return 0;
}


Measured memory_bytes: 256
#include<bits/stdc++.h>
using namespace std;
int main()
{
    int n;
    cin>>n;
    int a[n+1];
    for(int i=1;i<=n;i++){
        cin>>a[i];
    }
    int dp[n+1];
    dp[1]=0;
    dp[2]=abs(a[2]-a[1]);
    for(int i=3;i<=n;i++){
        int mn=INT_MAX;
        for(int j=1;j<=2;j++){
            mn=min(mn,dp[i-j]+abs(a[i]-a[i-j]));
        }
        dp[i]=mn;
    }
    cout<<dp[n];
}

Measured memory_bytes: 1024
#[allow(unused_macros)]
macro_rules! input {
    (source = $s:expr, $($r:tt)*) => {
        let mut iter = $s.split_whitespace();
        input_inner!{iter, $($r)*}
    };
    ($($r:tt)*) => {
        let s = {
            use std::io::Read;
            let mut s = String::new();
            std::io::stdin().read_to_string(&mut s).unwrap();
            s
        };
        let mut iter = s.split_whitespace();
        input_inner!{iter, $($r)*}
    };
}

#[allow(unused_macros)]
macro_rules! input_inner {
    ($iter:expr) => {};
    ($iter:expr, ) => {};

    ($iter:expr, $var:ident : $t:tt $($r:tt)*) => {
        let $var = read_value!($iter, $t);
        input_inner!{$iter $($r)*}
    };
}

#[allow(unused_macros)]
macro_rules! read_value {
    ($iter:expr, ( $($t:tt),* )) => {
        ( $(read_value!($iter, $t)),* )
    };

    ($iter:expr, [ $t:tt ; $len:expr ]) => {
        (0..$len).map(|_| read_value!($iter, $t)).collect::<Vec<_>>()
    };

    ($iter:expr, chars) => {
        read_value!($iter, String).chars().collect::<Vec<char>>()
    };

    ($iter:expr, usize1) => {
        read_value!($iter, usize) - 1
    };

    ($iter:expr, $t:ty) => {
        $iter.next().unwrap().parse::<$t>().expect("Parse error")
    };
}

fn ceil(x: usize, y:usize) -> usize {
    (x + y - 1) / y
}


fn main() {
    input! {
        n: usize,
        k: usize,
        a: [usize; n],
    }
    if k == 2 {
        println!("{}", n - 1);
        return;
    }
    if k == n {
        println!("{}", 1);
        return;
    }
    println!("{}", ceil(n-1, k - 1));
}

Measured memory_bytes: 6396
a,b,c = gets.split.map(&:to_i)

D = [a,b,c].sort

if (D[0]+D[1]) >= 10
  puts (D[2] + 1).to_s + (D[0]+D[1]-10).to_s
else
  puts (D[2].to_s) + (D[0]+D[1]).to_s
end


Measured memory_bytes: 1788
#include<iostream>
using namespace std;
int main()
{
    int N,i,j;
    cin>>N;
        for(i=10000; i>=0; i-=1000)
        {if(N%1000==0)
            j=0;
 else if(N%1000!=0)
            if(N<i)
                j=i-N;
        }
        cout<<j<<endl;

    return 0;
}


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

int main(){
    vector<int> l(3);
    for(int i=0;i<3;i++){
        cin >> l.at(i);
    }
    sort(l.begin(),l.end());
    int x=l.at(2)-l.at(1)+(l.at(1)-l.at(0))/2;
    if((l[1]-l[0])%2==0){
        cout << x << endl;
    }
    else{
        cout << x+2 << endl;
    }
}

Measured memory_bytes: 3544
#include<bits/stdc++.h>
using namespace std;
int main()
{
	long long a,b,t=1,c,d;
	cin>>a>>b;
	if(a*b<=0)
	cout<<"Zero"<<endl;
	else
	{
		if(a>0)
		cout<<"Positive"<<endl;
		else
		{
			if(abs(a-b)%2==0)
			cout<<"Negative"<<endl;
			else
			cout<<"Positive"<<endl;	
		}
		
	}
	return 0;
}

Measured memory_bytes: 256
def dot(A, B):
    ret = 0
    if len(A)==len(B):
        for a, b in zip(A, B):
            ret += a*b
    return ret
N, M, C = map(int, input().split())
B = list(map(int, input().split()))
A = [list(map(int, input().split())) for _ in range(N)]
ans = 0
for a in A:
    if dot(a, B)+C>0:
        ans += 1
print(ans)

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

using namespace std;
using namespace atcoder;

int main(int argc, const char *argv[]) {
  int n, m;
  cin >> n >> m;

  vector<vector<int>> g(n);
  scc_graph graph(n);
  for (int i = 0; i < m; ++i) {
    int a, b;
    cin >> a >> b;
    a--, b--;
    graph.add_edge(a, b);
    graph.add_edge(b, a);
  }

  auto scc = graph.scc();
  cout << scc.size() - 1 << '\n';
  return 0;
}

Measured memory_bytes: 13508
#include "iostream"
#include "climits"
#include "list"
#include "queue"
#include "stack"
#include "set"
#include "functional"
#include "algorithm"
#include "string"
#include "map"
#include "unordered_map"
#include "unordered_set"
#include "iomanip"
#include "cmath"
#include "random"
#include "bitset"
#include "cstdio"
#include "numeric"
#include "cassert"
#include "functional"
#include "ctime"

using namespace std;

//constexpr long long int MOD = 1000000007;
//constexpr int MOD = 1000000007;
constexpr int MOD = 998244353;
//constexpr long long int MOD = 998244353;
constexpr double EPS = 1e-8;

//int N, M, K, H, W, L, R;
long long int N, M, K, H, W, L, R;


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

	cin >> N >> M >> K;
	vector<int>v(N);
	for (int i = 0; i < K; i++) {
		cin >> L;
		v[L - 1]++;
	}
	for (int i = 0; i < N; i++) {
		if (M - K + v[i] > 0)cout << "Yes\n";
		else cout << "No\n";
	}
}

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

int main(){
    int n;
    cin >> n;

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

    long long int takahashi = t[0], aoki = a[0];
    for(int i=1; i<n; i++){
        long long int coeff = max((takahashi + t[i] - 1) / t[i], 
                                  (aoki      + a[i] - 1) / a[i]);
        takahashi = coeff * t[i];
        aoki      = coeff * a[i];
    }
    cout << takahashi + aoki << endl;
    return 0;
}

Measured memory_bytes: 256
// "header" {{{
#include <iostream>
#include <vector>
#include <algorithm>
#include <unordered_map>
#include <string>
#include <deque>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <cstring>
#include <iomanip>
#include <tuple>

using namespace std;

#define rep(i,n) for(int i=0;i<(n);++i)
#define reps(i,n) for(int i=1;i<=(n);++i)
#define all(x) (x).begin(),(x).end()
#define pii pair<int,int> 
//#define int long long
#define setout(n,x) setw(n) << setfill(x)
#define Fixed fixed << setprecision(10)
const int INF = 0x3f3f3f3f;
const long long LINF = 0x3f3f3f3f3f3f3f3fLL;
const long long mod = 1000000007;

inline double sqrt( const double &x) 
{
	double         xHalf = 0.5 * x;
	int64_t  tmp   = 0x5FE6EB50C7B537AAl - ( *(int64_t*)&x >> 1);
	double         xRes  = * (double*)&tmp;

	xRes *= ( 1.5 - ( xHalf * xRes * xRes ) );
	xRes *= ( 1.5 - ( xHalf * xRes * xRes ) );
	xRes *= ( 1.5 - ( xHalf * xRes * xRes ) );
	xRes *= ( 1.5 - ( xHalf * xRes * xRes ) );
	return xRes * x;
}
// }}}

signed main(){
	cin.tie(0);
	ios::sync_with_stdio(false);
	int n;
	cin >> n;
	int sum = 0;
	rep(i,n){
		int a;
		cin >> a;
		sum += __builtin_ctz(a);
	}
	cout << sum << '\n';
	return 0;	
}


Measured memory_bytes: 256
def main():
	a , b = map(int , input().split())
	a=a-1
	rem = a%4
	x, y =0 ,0
	if rem==0:
		x=a
	elif rem==1:
		x=1
	elif rem==2:
		x = a+1
	else:
		x=0
	rem = b%4
	if rem ==0:
		y=b
	elif rem==1:
		y=1
	elif rem==2:
		y=b+1
	else:
		y=0
	print(x^y)
if __name__=="__main__":
	main()

Measured memory_bytes: 61728
#include <stdio.h>
#include <string.h>

int count_AB(char[], int n);


int main(void)
{
	char s[12];
	int A = 0, B = 0, AB = 0;
	int N, L = 0,C = 0;
	scanf("%d", &N);
	for (int i = 0; i < N; i++) {
		scanf("%s", s);
		L = strlen(s);
		C += count_AB(s, L);
		if (s[0] == 'B' && s[L - 1] == 'A') {
			AB++;
			A++;
			B++;
		}
		else if (s[0] == 'B' && s[L - 1] != 'A') {
			B++;
		}
		else if (s[0] != 'B' && s[L - 1] == 'A') {
			A++;
		}
		else {
			continue;
		}
	}
	if (AB == B && B == A && A != 0) {
		printf("%d", C += AB - 1);
	}
	else if (A > B) {
		printf("%d", C += B);
	}
	else {
		printf("%d", C += A);
	}
	return 0;
}

int count_AB(char x[], int n)
{
	int i = 0, count = 0;
	while (i < n) {
		if (x[i] == 'A' && x[i + 1] == 'B') {
			count++;
		}
		i++;
	}
	return count;
}

Measured memory_bytes: 128
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll,ll> pl;
#define all(a) (a).begin(),(a).end()
#define overload4(_1, _2, _3, _4, name, ...) name
#define rep1(n) for(ll i = 0;i < (n);i++)
#define rep2(i,n) for(ll i = 0;i < (n);i++)
#define rep3(i,a,b) for(ll i = (a);i < (b);i++)
#define rep4(i,a,b,c) for(ll i = (a);i < (b);i += (c))
#define rep(...) overload4(__VA_ARGS__, rep4, rep3, rep2, rep1)(__VA_ARGS__)
#define rrep1(n) for(ll i = (n)-1;i >= 0;i--)
#define rrep2(i,n) for(ll i = (n)-1;i >= 0;i--)
#define rrep3(i,a,b) for(ll i = (a)-1;i >= (b);i--)
#define rrep4(i,a,b,c) for(ll i = (a)-1;i >= (b);i -= (c))
#define rrep(...) overload4(__VA_ARGS__, rrep4, rrep3, rrep2, rrep1)(__VA_ARGS__)
#define SHOW(x) cout << #x << ": " << x << endl;
template<class T> ll sumArr(const T& a){ return accumulate(all(a), 0LL);}
template<class T> auto minArr(const T& a){ return *min_element(all(a));}
template<class T> auto maxArr(const T& a){ return *max_element(all(a));}
template<class T> void DEBUG_PRINT(T tmp){cout<<tmp<<endl;}
template<class T1,class... T2> void DEBUG_PRINT(T1 tmp,T2... tmp2){cout<<tmp<<" ";DEBUG_PRINT(tmp2...);}
ll gcd(ll a, ll b){ while(b){ ll c = b; b = a % b; a = c; } return a; }
const ll INF=1e18,MOD = 1e9+7;
const int iINF = 1e9;


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

    int n,ans = 0;
    cin>>n;
    while(ans< n)ans += 1000;
    cout<<ans - n<<endl;
   
    return 0;
}


Measured memory_bytes: 3584
#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 - 1); i >= 0; --i)
#define FOR(i, x, n) for (int i = x; i < (int)(n); ++i)
#define ALL(n) begin(n), end(n)
#define ll long long
#define INF 1e9
int main() {
    string s, t;
    cin >> s >> t;
    bool ans = true;
    REP(i, s.size()) {
        if (s[i] != t[i]) ans = false;
    }
    if (t.size() != s.size() + 1) ans = false;

    if (ans)
        cout << "Yes" << endl;
    else
        cout << "No" << endl;

    return 0;
}

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

int a, b, c[101][101];

int main(){
//	cin.tie(0);
//	ios::sync_with_stdio(false);
	cin >> a >> b;
	for(int i=50; i<100; ++i)
		for(int j=0; j<100; ++j)
			c[i][j] = 1;
	for(int i=0; i<a-1; ++i) c[i/49*2+51][i%49*2+1] = 0;
	for(int i=0; i<b-1; ++i) c[i/49*2+1][i%49*2+1] = 1;
	cout << "100 100\n";
	for(int i=0; i<100; ++i)
		for(int j=0; j<100; ++j)
			cout << (c[i][j] ? '#' : '.') << (j == 99 ? "\n" : "");
}

Measured memory_bytes: 256
import itertools
import math
N = int(input())
A =[0]*N
B =[0]*N
su =0
count=0
for i in range(N):
  A[i],B[i] =map(int, input().split())
for i in itertools.permutations(range(N)):
  count +=1
  for j in range(1,len(i)):
    su += math.sqrt((A[i[j]]-A[i[j-1]])**2+(B[i[j] ]-B[i[j-1]])**2)
print(su/count)
    


Measured memory_bytes: 3064
#include <iostream>
#include <vector>
#include <algorithm>
#define inf 1000000000

using namespace std;

struct edge{
	int from, to, cost;
	edge(int a, int b, int c){
		from = a, to = b, cost = c;
	}
	bool operator<(const edge& ope){
		return cost < ope.cost;
	}
};

int N, M;
vector<int> mst;
vector<edge> G, g;

int parent[505];

void init()
{
	for(int i = 1; i <= N; i++) parent[i] = i;
}

int root(int i)
{
	if(parent[i] == i) return i;
	return parent[i] = root(parent[i]);
}

bool same(int i, int j)
{
	return root(i) == root(j);
}

void unite(int i, int j)
{
	int root_i = root(i), root_j = root(j);
	if(root_i == root_j) return;
	parent[root_i] = root_j;
}

int getMST(vector<edge> &G)
{
	int ret = 0;
	init();
	for(int i = 0; i < G.size(); i++){
		if(!same(G[i].from, G[i].to)){
			ret += G[i].cost;
			unite(G[i].from, G[i].to);
		}
	}
	return ret;
}

int main(void)
{
	cin >> N >> M;
	
	int s, d, c;
	for(int i = 0; i < M; i++){
		cin >> s >> d >> c;
		G.push_back( edge(s, d, c) );
	}
	sort(G.begin(), G.end());
	
	init();
	int min_cost = 0;
	for(int i = 0; i < G.size(); i++){
		if(!same(G[i].from, G[i].to)){
			min_cost += G[i].cost;
			unite(G[i].from, G[i].to);
			mst.push_back(i);
		}
	}
	
	int ans_cnt = 0, ans_cost = 0, tmp;
	for(int i = 0; i < mst.size(); i++){
		g = G;
		g[mst[i]].cost = inf;
		sort(g.begin(), g.end());
		if(getMST(g) > min_cost){
			ans_cnt++;
			ans_cost += G[mst[i]].cost;
		}
	}
	
	cout << ans_cnt << " " << ans_cost << endl;
	return 0;
}

Measured memory_bytes: 4252
#define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
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;}
#define rep(i,n) for(int i=0;i<(int)(n);i++)
#define all(vec) vec.begin(),vec.end()
typedef long long ll;
typedef pair<ll,ll> l_l;
typedef pair<int,int> i_i;
const ll MOD=1e9+7;
const int INF=1<<30;

int main(){
  int A,B;
  cin >> A >> B;
  if(A>=13){
    cout << B << endl;
  }
  else if(A<6){
    cout << 0 << endl;
  }
  else{
    cout << B/2 << endl;
  }
}

Measured memory_bytes: 256
#include <bits/stdc++.h>
using namespace std;
#define FOR(i,n) for(int i=0;i<(n);i++)
#define all(t) (t).begin(),(t).end()
#define MOD 1000000007
typedef long long ll;
template <class T> inline bool chmax(T& a,T b){if(a<b){a=b;return 1;}else{return 0;}}
template <class T> inline bool chmin(T& a,T b){if(a>b){a=b;return 1;}else{return 0;}}
template <class T> inline T GCD(T a,T b){T c;while(b!=0){c=a%b;a=b;b=c;}return a;}
template <class T> inline T LCM(T a,T b){return a*b/GCD(a,b);}

int main(){
	int n,a,b,c;
	string s;
	cin >> n;
	if(n%2==0)n--;
	cout << n/2 << endl;
}


Measured memory_bytes: 764
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using vl = vector<ll>;
using vvl = vector<vl>;
using pll = pair<ll, ll>;
using vpll = vector<pll>;
using vb = vector<bool>;
using vs = vector<string>;
const ll oo = 0x3f3f3f3f3f3f3f3fLL;
const ld eps = 1e-9;
#define sz(c) ll((c).size())
#define all(c) begin(c), end(c)
#define mp make_pair
#define pb push_back
#define eb emplace_back
#define xx first
#define yy second
#define FOR(i,a,b) for (ll i=(a); i<(b); i++)
#define FORD(i,a,b) for(ll i=ll(b)-1; i>=(a); i--)
#define TR(X) ({if(1) cerr << "TR: " << (#X) << " = " << (X) << endl;})

int main(){
	cin.sync_with_stdio(0);

	ll n, k;
	cin >> n >> k;
	vl h(n);
	vl stein(n);

	FOR(i,0,n){
		cin >> h[i]; 
	}

	stein[0] = 0;

	FOR(i,1,n){
		if(i > k){
			ll tmp = oo;
			FOR(j,1,k+1){
				tmp = min(tmp, (stein[i - j] + abs(h[i] - h[i - j])));
			}
			stein[i] = tmp;
		}else{
			stein[i] = stein[0] + abs(h[0] - h[i]);
		}
	}

	cout << stein[n - 1] << endl;
}


Measured memory_bytes: 1792
#include <bits/stdc++.h>
using namespace std;
int main() {
    int n, d;
    cin >> n >> d;

    int a = 2 * d + 1;
    int res = n / a;
    if (n % a == 0) {
        cout << res;
    } else {
        cout << res + 1;
    }
}

Measured memory_bytes: 256
#include<stdio.h>
int main()
{
    int a,b,k,i;
    scanf("%d %d %d",&a,&b,&k);
    for(i=a;i<=b;i++)
        if(i-a<=k-1||b-i<=k-1)
        printf("%d\n",i);
    return 0;
}


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

#define ll long long
#define endl "\n";
#define mod 1000000007
#define pii pair<int,int>
#define vii vector<pii>
#define mp make_pair
#define all(a) a.begin(), a.end()
#define mk(arr,n,type) type *arr=new type[n];

void fastio() {
    ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#endif
}

int main() {
    fastio();
    char ch;
    cin >> ch;

    if (ch >= 'A' && ch <= 'Z') cout << 'A';
    if (ch >= 'a' && ch <= 'z') cout << 'a';
    return 0;
}


Measured memory_bytes: 3648
#include "bits/stdc++.h"
using namespace std;
 
int main(void) {
    int input, ans = 0, ans2 = 234, ans3 = 567, flag;
    cin >> input;
 
    vector<int> side(input);
    for (int i = 0; i < input; i++) {
        cin >> side.at(i);
    }
 
    sort(side.begin(), side.end());
    reverse(side.begin(), side.end());
 
    vector<int> num;
    num.push_back(0);
    for (int i = 0; i < input; i++) {
        flag = 0;
        for (int j = i + 1; j < input; j++) {
            for (int k = j + 1; k < input; k++) {
                if (side.at(i) < side.at(j) + side.at(k) && side.at(j) < side.at(k) + side.at(i) && side.at(k) < side.at(i) + side.at(j)) {
                    ans++;
                    ans2+=2;
                  	ans3+=3;
                }
                else {
                    break;
                    flag++;
                }
            }
            if (flag != 0) {
                break;
            }
        }
    }
 
    cout << ans << endl;
 
}

Measured memory_bytes: 3612
#include <bits/stdc++.h>
#define int long long
using pii=std::pair<int,int>;
using namespace std;

const int maxn = 5005;

int n, k, p[maxn], c[maxn], visited[maxn];

int getans(int x, int rem, vector<int>& path)
{
    assert(x < n);
    if(!rem)
        return 0;
    int ans;
    if(!visited[x])
    {
        visited[x] = 1;
        path.push_back(x);
        ans = c[x] + getans(p[x], rem - 1, path);
        if(rem < k)
            ans = max(ans, 0ll);
    }
    else
    {
        int start = 0;
        while(start < path.size() && path[start] != x)
            start++;
        assert(start < path.size());
        vector<int> cyclevals;
        while(start < path.size())
        {
            cyclevals.push_back(path[start]);
            start++;
        }
        int cyclesz = cyclevals.size(), cyclesum = 0, bestprefix = 0, bestprefixsize = 0;
        vector<int> prefixes(cyclesz, -2e18);
        for(int i = 0; i < cyclesz; i++)
        {
            cyclesum += c[cyclevals[i]];
            prefixes[i] = cyclesum;
            if(cyclesum > bestprefix)
            {
                bestprefix = cyclesum;
                bestprefixsize = i + 1;
            }
        }
        if(cyclesum <= 0)
        {
            ans = 0;
            int steps = min(rem, cyclesz);
            for(int j = 0; j < steps; j++)
                ans = max(ans, prefixes[j]);
        }
        else
        {
            ans = 0;
            int times = 0;
            if(rem >= bestprefixsize)
            {
                times = (rem - bestprefixsize) / cyclesz;ans = times * cyclesum + bestprefix;
                ans = times * cyclesum + bestprefix;
            }
            times = rem / cyclesz;
            int extra = rem % cyclesz;
            int bestextra = 0;
            for(int j = 0; j < extra; j++)
                bestextra = max(bestextra, prefixes[j]);
            ans = max(ans, times * cyclesum + bestextra);
        }
    }
    return ans;
}

int32_t main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cin >> n >> k;
    for(int i = 0; i < n; i++)
    {
        cin >> p[i];
        p[i]--;
    }
    for(int i = 0; i < n; i++)
        cin >> c[i];
    int ans = -2e18;
    for(int i = 0; i < n; i++)
    {
        vector<int> path;
        for(int j = 0; j < n; j++)
            visited[j] = 0;
        int curans = getans(i, k, path);
        ans = max(ans, curans);
    }
    cout << ans << "\n";
    return 0;
}

Measured memory_bytes: 4232
#include <iostream>
#include <stdio.h>
#include <algorithm>
#include <string.h>
#include <math.h>
#define ll long long
using namespace std;
int a[100005],b[5];
int main()
{
    int n;
    ll s;
    b[0]=0;
    b[1]=0;
    scanf("%d",&n);
    for(int i=0;i<n;i++)
    {
        scanf("%d",&a[i]);
    }
    sort(a,a+n);
    int g=0;
    for(int j=n-1;j>=0; )
    {
        if(g<2)
        {
            if(a[j]==a[j-1])
            {
                b[g]=a[j];
                g++;
                j=j-2;
                if(g==2)
                    break;
            }
            else
                j=j-1;
        }
    }
    s=(ll)b[0]*b[1];
    printf("%lld\n",s);
    return 0;
}


Measured memory_bytes: 640
N, A, B, C, D = map(int, input().split())
l = A - (N-1) * D
r = A - (N-1) * C
if l <= B <= r:
    print('YES')
    exit()
for _ in range(N):
    l = r + 2*C + (N-2) * (C-D)
    r = l + (N-1) * (D-C)
    if l <= B <= r:
        print('YES')
        exit()
print('NO')


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

int main() {
  int h, w;
  while (true) {
    cin >> h >> w;
    if (h == 0) return 0;
    for (int i = 0; i < h; i ++) {
      for (int j = 0; j < w; j ++) {
        cout << '#';
      }
      cout << endl;
    }
    cout << endl;
  }
}



Measured memory_bytes: 3080
#include <bits/stdc++.h>
using namespace std;
#define ll long long
int const N = 3e3 + 5;
string s, t;
int n, m;
int mem[N][N];
int oo = 0x3f3f3f3f;
int DP(int i=0, int j = 0){
    if(i == n || j == m)
        return 0;
    int &ret = mem[i][j];
    if(~ret)
        return ret;
    return ret = max(DP(i+1, j), max(DP(i, j+1), (s[i]==t[j]?DP(i+1, j+1) + 1: 0 )) );
}
string res;
void print(int i=0, int j=0){
    if(i == n || j == m)
        return ;
    if(DP(i, j) == DP(i+1, j))
        return print(i+1, j);
    else if(DP(i, j) == DP(i, j+1))
        return print(i, j+1);
    else {
        res.push_back(s[i]);
        print(i+1, j+1);
    }
}

int main() {
#ifdef ONLINE_JUDGE
    ios::sync_with_stdio(false), cin.tie(0);
#endif
#ifndef ONLINE_JUDGE
    freopen("in.txt", "r", stdin);
    freopen("out.txt", "w", stdout);
#endif
    memset(mem, -1, sizeof(mem));
    cin>>s>>t;
    n = s.size(), m = t.size();
    DP(0, 0);
    print();
    cout<<res<<endl;
}

Measured memory_bytes: 39536
#include <cstdio>

#define N ( 'k' - 'a' + 1 )

using namespace std;

static char * e;
static char   s[ 1024 ];

static int v[ N ];

static int
formula (
  void
  )
{
  int lt, rt;

  switch ( *e )
  {
    case 'T':
      ++e;
      return ( 1 );
    case 'F':
      ++e;
      return ( 0 );
    case 'a':
    case 'b':
    case 'c':
    case 'd':
    case 'e':
    case 'f':
    case 'g':
    case 'h':
    case 'i':
    case 'j':
    case 'k':
      ++e;
      return ( v[ e[ -1 ] - 'a' ] );
    case '-':
      ++e;
      return ( !formula ( ) );
    case '(':
      ++e;
      lt = formula ( );
      switch ( *e )
      {
        case '*':
          ++e;
          rt = formula ( );
          ++e;
          return ( lt && rt );
        case '+':
          ++e;
          rt = formula ( );
          ++e;
          return ( lt || rt );
        case '-':
          e += 2;
          rt = formula ( );
          ++e;
          return ( !lt || rt );
      }
  }
}

static int
equation (
  void
  )
{
  const int lt = formula ( );

  ++e;
  return ( lt == formula ( ) );
}

static int
test (
  const int r
  )
{
  if ( r == N )
  {
    e = s;
    return
    equation ( );
  }

  v[ r ] = 0;
  if ( !test ( r + 1 ) ) return ( 0 );
  v[ r ] = 1;
  if ( !test ( r + 1 ) ) return ( 0 );

  return ( 1 );
}

/** Application main entry point. */
int
main (
  int     argc,
  char  * argv[ ]
  )
{
  for ( ; ; )
  {
    scanf ( " %s", s );
    if ( *s == '#' ) break ;

    puts ( test ( 0 ) ? "YES" : "NO" );
  }

  return ( 0 );
}

Measured memory_bytes: 740
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,int> P;
int main(){
  int sum=0,a,b,c,k;
  cin >> a >> b >> c >> k;
  while(a>=b){
    sum++;
    b=b*2;
  }
  while(b>=c){
    sum++;
    c=c*2;
  }
  if(sum<=k){
    cout << "Yes" << endl;
  }
  else{
    cout << "No" << endl;
  }
}

Measured memory_bytes: 3592
import java.util.*;
public class Main 
{
	public static void main(String[] args) 
	{
		Scanner stdIn = new Scanner(System.in);
		int m = stdIn.nextInt();
		while(m-- != 0)
		{
			String str = stdIn.next();
			int size = str.length();
			Set<String> set = new HashSet<String>();
			for(int i = 1; i < size; ++i)
			{
				String first = "";
				String second = "";
				for(int j = 0; j < i; ++j)
				{
					first += str.charAt(j);
				}
				for(int j = i; j < size; ++j)
				{
					second += str.charAt(j);
				}
				StringBuilder revFirst = new StringBuilder(first);
				StringBuilder revSecond = new StringBuilder(second);
				revFirst.reverse();
				revSecond.reverse();
				set.add(first + second);
				set.add(first + revSecond.toString());
				set.add(revFirst.toString() + second);
				set.add(revFirst.toString() + revSecond.toString());
				set.add(second + first);
				set.add(second + revFirst.toString());
				set.add(revSecond.toString() + first);
				set.add(revSecond.toString() + revFirst.toString());
			}
			System.out.println(set.size());
		}
	}
}

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

int main(){
 map<char , int >mp;
 string s;
 cin >> s;
 for(int i=0;i<s.size();i++){
   mp[s[i]]++;
 }
 map<char , int >::iterator it ;
 for(it=mp.begin();it!=mp.end();it++){
   if(it->second%2!=0){
    cout << "No" <<endl;
    return 0;
   }
 }
 cout << "Yes" <<endl;
 return 0;
}


Measured memory_bytes: 3600
#include <iostream>
using namespace std;
#define MAX 10005
#define MAXL 1000000
//const MAX  = 10001;
int ans[MAX];

int main(){
	int n;
	static bool c[MAXL]={true, true};
	for(int i = 2;i < MAXL;i++){
		if(!c[i]){
			for(int j = 2;i*j < MAXL;j++)c[i*j] = true;
		}
	}
	int j = 1;
	for(int i = 1;j < MAX;i++){
		if(!c[i])ans[j] += ans[j-1] + i, j++;
	}
	//for(int i = 0;i < 10;i++)cout << ans[i] << " ";
	while(cin >> n, n){
		cout << ans[n] << endl;
	}
	return 0;
}
/*
2
9
0
*/

Measured memory_bytes: 4112
#include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int i=0;i<(int)(n);i++)
#define REP(i,m,n) for(int i=m;i<(int)(n);i++)
typedef pair<int,int> pint;
typedef long long ll;
#define SORT(a) sort((a).begin(), (a).end())
#define RSORT(a) reverse((a).begin(), (a).end())
void solve(long long N, long long T, std::vector<long long> A){
    map<ll,int> l{};
    ll ma=0;
    ll madif=0;
    RSORT(A);
    rep(i,N){
        ma=max(ma,A[i]);
        if (0<ma-A[i]){
            l[ma-A[i]]+=1;
            madif=max(madif,ma-A[i]);
        }
    }
    
    cout<<l[madif]<<endl;
}

// Generated by 1.1.6 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
int main(){
    long long N;
    scanf("%lld",&N);
    long long T;
    scanf("%lld",&T);
    std::vector<long long> A(N);
    for(int i = 0 ; i < N ; i++){
        scanf("%lld",&A[i]);
    }
    solve(N, T, std::move(A));
    return 0;
}


Measured memory_bytes: 7296
#include<bits/stdc++.h>
using namespace std;
#define REP(i,n) for(long long i=0;i<(n);i++)
#define REP1(i,n) for(long long i=1;i<=(n);i++)
#define REP2D(i,j,h,w) for(long long i=0;i<(h);i++) for(long long j=0;j<(w);j++)
#define REP2D1(i,j,h,w) for(long long i=1;i<=(h);i++) for(long long j=1;j<=(w);j++)
#define PER(i,n) for(long long i=((n)-1);i>=0;i--)
#define PER1(i,n) for(long long i=(n);i>0;i--)
#define FOR(i,a,b) for(long long i=(a);i<(b);i++)
#define FORE(i,a,b) for(long long i=(a);i<=(b);i++)
#define ITE(arr) for(auto ite=(arr).begin();ite!=(arr).end();++ite)
#define ALL(a) ((a).begin()),((a).end())
#define RANGE(a) (a),((a)+sizeof(a))
#define ZERO(a) memset(a,0,sizeof(a))
#define MINUS(a) memset(a,0xff,sizeof(a))
#define YNPRT(b) cout<<((b)?"Yes":"No")<<endl
#define ENTER printf("\n")
#define REV(arr) reverse(ALL(arr))
#define PRT(a) cout<<(a)<<endl
#ifdef DEBUG
#define DBPRT(a) cout << "[Debug] - " << #a << " : " << (a) << endl
#define DBSTART if(1){
#define DBEND }
#else
#define DBPRT(a) do{}while(0)
#define DBSTART if(0){
#define DBEND }
#endif
#define PRTLST(arr,num) REP(_i,num) cout<<_i<<" - "<<arr[_i]<<endl;
#define PRTLST2(arr2,d1,d2) REP(_i,d1) REP(_j,d2) cout<<_i<<","<<_j<<" : "<<arr2[_i][_j]<<endl;
#define PRTLST2D(arr2,d1,d2) do{cout<<"L\t";REP(_i,d2) cout<<_i<<"\t"; cout<<endl; REP(_i,d1){cout<<_i<<"\t";REP(_j,d2){cout<<arr2[_i][_j]<<"\t";}cout<<endl;}}while(0);

#define TOSUM(arr,sum,n) {sum[0]=arr[0];REP1(i,n-1) sum[i]=sum[i-1]+arr[i];}

#define MIN(target,v1) (target)=min(target,v1)
#define MAX(target,v1) (target)=max(target,v1) 
#define P1 first
#define P2 second
#define PB push_back
#define UB upper_bound
#define LB lower_bound
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
const int INF_INT = 2147483647;
const ll INF_LL = 9223372036854775807LL;
const ull INF_ULL = 18446744073709551615Ull;
const ll P = 92540646808111039LL;
const int Move[4][2] = {-1,0,0,1,1,0,0,-1};
const int Move_[8][2] = {-1,0,-1,-1,0,1,1,1,1,0,1,-1,0,-1,-1,-1};
template<typename T1,typename T2>
ostream& operator<<(ostream& os,const pair<T1,T2>& p) {os<<"( "<<p.P1<<" , "<<p.P2<<" )";return os;}
template<typename T>
ostream& operator<<(ostream& os,const vector<T>& v) {ITE(v) os << (ite-v.begin()) << " : " << *ite <<endl;return os;}
template<typename T>
ostream& operator<<(ostream& os,const set<T>& v) {os<<" { ";ITE(v) {os<<*ite;if(ite!=--v.end()){os<<" , ";}} os<<" } ";return os;}
template<typename T1,typename T2>
ostream& operator<<(ostream& os,const map<T1,T2>& m) {ITE(m) {os<<ite->P1<<"\t\t|->\t\t"<<ite->P2<<endl;} return os;}

//---------------------
#define MAXN 100000
//---------------------

ll n;
string s;

int main(){
	cin >> n >> s;
	bool res = false;
	DBPRT(s);
	if(n%2==0){
		res = true;
		REP(i,n>>1){
			if(s[i]!=s[i+n/2]) {res = false; break;}
		}
	}
	YNPRT(res);
	return 0;
}









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


int main(){
    string s;
    cin >> s;
    int cnt0=0,cnt1=0;

    for(int i=0;i<s.size();i++){
        if(s[i]=='0'){
            cnt0++;
        }else{
            cnt1++;
        }
    }

    cout << min(cnt0,cnt1)*2 << endl;
}

Measured memory_bytes: 3736
#!/usr/bin/env python3
import sys


YES = "Yes"  # type: str
NO = "No"  # type: str


def solve(N: int):
    sN = sum(int(d) for d in str(N))
    print(YES if N % sN == 0 else NO)


def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    solve(N)


if __name__ == '__main__':
    main()


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

int main() {

	int a, b, c;
	int count = 0;
	int i;

	cin >> a >> b >> c;

	for (i = a; i <= b; i++) {

		if (c%i == 0) count = count + 1;

	}

	cout << count << endl;

	return 0;
}

Measured memory_bytes: 3048
#include<iostream>
#include<cstdio>
#include<queue>
#include<string>
#include<cstring>

using namespace std;

#define MAXM 500010
#define MAXN 10010
#define ANS_MAX 2147483647
#define INF 1<<27
struct EDGE{
	int next;
	int to;
	int w;
//	EDGE(int _next,int _to,int _w) : next(_next) ,to(_to),w(_w){
//	};
};

EDGE edge[MAXM];

int n,m ,st,cnt;
int head[MAXN];

int d[MAXN],outqueue[MAXN];
bool inq[MAXN];
queue<int> Q;

void Add(int u,int v,int w){
	edge[++cnt].next = head[u];
	edge[cnt].to = v;
	edge[cnt].w = w;
	head[u] = cnt;
}


inline int Read(){
	char c;
	int ans = 0;
	bool Sign = false;
	while(!isdigit(c=getchar()) && c != '-');
	if(c == '-') {
		Sign = true;
		c = getchar();
	}
	do {
		ans = (ans<<3) + (ans<<1) + (c - '0');
	} while(isdigit(c=getchar()));
	return Sign ? -ans : ans;
}

void read(){
	int x,y,w;
	n = Read();
	m = Read();
	st = Read();
	for(int i=1;i<=m;i++){
		x = Read();
		y = Read();
		w = Read();
		Add(x,y,w);
	}
}
int SPFA(int x){
	int k;
	for(int i=0;i<=n;i++) d[i] = ANS_MAX;
	d[x] = 0;
	Q.push(x);
	inq[x] = true;
	while(!Q.empty()){
		k = Q.front();
		Q.pop();
		inq[k] = false;
		outqueue[k]++;
		if(outqueue[k]>n) return 0;
		if(d[k] == ANS_MAX) continue;
		for(int i =head[k];i!=0;i=edge[i].next){
			int j = edge[i].to;
			if(d[j]>d[k]+edge[i].w){
				d[j] = d[k] + edge[i].w;
				if(!inq[j]){
					Q.push(j);
					inq[j] = true;
				}
			}
		}
	}
	return 1;
	
}

int main(){
	memset (outqueue,0,sizeof(outqueue));
	read();
	if(SPFA(st)) 
	for(int i=0; i<n; i++) 
	{
		if(d[i]==ANS_MAX) cout<<"INF\n";
		else
		printf("%d\n", d[i]);
	}
	else
	cout<<"NEGATIVE CYCLE\n";
	return 0;
}


Measured memory_bytes: 3324
#include<stdio.h>
int main()
{
    long long n,i,a=0,w=0,t=0,r=0;
    char str[10];
    scanf("%lld",&n);
    for(i=0;i<n;i++)
    {
        scanf("%s",&str);
        if(str[0]=='A')
            a++;
        else if(str[0]=='W')
            w++;
        else if(str[0]=='T')
            t++;
        else if(str[0]=='R')
            r++;
    }
    printf("AC x %lld\n",a);
    printf("WA x %lld\n",w);
    printf("TLE x %lld\n",t);
    printf("RE x %lld\n",r);
    return 0;
}


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

int main() {
  int K;
  string S;
  cin >> K >> S;
  
  int i = 0;
  while(i < K) {
    cout << S.at(i);
    i++;
    if(i == S.size()) break;
  }
  
  if(S.size() > i) cout << "...";
  
  cout << endl;
}

Measured memory_bytes: 3632
//include
//------------------------------------------
#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cctype>
#include <string>
#include <cstring>
#include <ctime>
#include <climits>
#include <queue>

using namespace std;

//typedef
//------------------------------------------
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef long long LL;

//container util
//------------------------------------------
#define ALL(a)  (a).begin(),(a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define PB push_back
#define MP make_pair
#define SZ(a) int((a).size())
#define EACH(i,c) for(typeof((c).begin()) i=(c).begin(); i!=(c).end(); ++i)
#define EXIST(s,e) ((s).find(e)!=(s).end())
#define SORT(c) sort((c).begin(),(c).end())

//repetition
//------------------------------------------
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n)  FOR(i,0,n)

//constant
//--------------------------------------------
const double EPS = 1e-10;
const double PI  = acos(-1.0);

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

  string str; getline(cin, str);
  int ans; cin >> ans;
  
  VI num;
  for(int i=0;i<SZ(str);i+=2)
	num.PB(str[i] - '0');

  int calcM = 0;
  VI tmp = num;
  for(int i=1;i<SZ(str);i+=2)
	if(str[i] == '*'){
	  tmp[i/2+1] = tmp[i/2] * tmp[i/2+1];
	  tmp[i/2] = 0;
	}
  calcM = accumulate(ALL(tmp), 0);

  int calcL = num[0];
  for(int i=1;i<SZ(str);i+=2)
	if(str[i] == '+') calcL += num[i/2+1];
	else calcL *= num[i/2+1];

  if(calcM == calcL)
	cout << (ans == calcM? "U": "I") << endl;
  else
	cout << (ans == calcM? "M": (ans == calcL? "L": "I")) << endl;
  
  return 0;
}

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

using VI = vector<int>;
using VVI = vector<VI>;
using PII = pair<int, int>;
using LL = long long;
using VL = vector<LL>;
using VVL = vector<VL>;
using PLL = pair<LL, LL>;
using VS = vector<string>;

#define ALL(a)  begin((a)),end((a))
#define RALL(a) (a).rbegin(), (a).rend()
#define PB push_back
#define EB emplace_back
#define MP make_pair
#define SZ(a) int((a).size())
#define SORT(c) sort(ALL((c)))
#define RSORT(c) sort(RALL((c)))
#define UNIQ(c) (c).erase(unique(ALL((c))), end((c)))

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

#define FF first
#define SS second
template<class S, class T>
istream& operator>>(istream& is, pair<S,T>& p){
  return is >> p.FF >> p.SS;
}
template<class S, class T>
ostream& operator<<(ostream& os, const pair<S,T>& p){
  return os << p.FF << " " << p.SS;
}
template<class T>
void maxi(T& x, T y){
  if(x < y) x = y;
}
template<class T>
void mini(T& x, T y){
  if(x > y) x = y;
}


const double EPS = 1e-10;
const double PI  = acos(-1.0);
const LL MOD = 1e9+7;
const int INF = 1e9;

struct E {
  int to, len, cost, group;
  E(int t, int l, int c, int g)
	:
	to(t),len(l),cost(c),group(g){
  }
};
using Graph = vector<vector<E>>;

void add_edge(Graph& G, int from, int to, int len, int cost, int g){
  G[from].EB(to, len, cost, g);
  G[to].EB(from, len, cost, g);
}

int dist[256][100][25];
int bit[256];
void Dijkstra(const Graph& G, int S, int B, int H){
  using K = pair<int,PII>;
  priority_queue<K,vector<K>,greater<K>> pq;
  dist[B][S][0] = 0;
  pq.push(MP(0,MP(S,0)));
  
  while(!pq.empty()){
	int co = pq.top().FF;
	auto ut = pq.top().SS;
	pq.pop();

	if(dist[B][ut.FF][ut.SS] < co) continue;
	for(auto& e: G[ut.FF]){
	  int nc = co + (B>>e.group&1? 0: e.cost);
	  int nt = ut.SS + e.len;
	  if(nt > H || nc >= dist[B][e.to][nt]) continue;
	  dist[B][e.to][nt] = nc;
	  pq.push(MP(nc, MP(e.to, nt)));
	}
  }
}

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

  int N, M, K, H;
  while(cin >> N >> M >> H >> K, N){
	Graph G(N);
	REP(i,M){
	  int a, b, c, d, e;
	  cin >> a >> b >> c >> d >> e;
	  --a;
	  --b;
	  --e;
	  add_edge(G, a, b, d, c, e);
	}

	int S, T;
	cin >> S >> T;
	--S;
	--T;

	fill((int*)dist, (int*)dist+256*100*25, INF);
	for(int b=0;b<(1<<K);++b){
	  Dijkstra(G, S, b, H);
	}

	int P;
	cin >> P;
	fill(bit, bit+256, INF);
	bit[0] = 0;
	REP(p,P){
	  int l, d;
	  cin >> l >> d;
	  int b = 0;
	  REP(i,l){
		int x;
		cin >> x;
		b |= 1 << (x-1);
	  }

	  mini(bit[b], d);

	  for(int m=b;m>0;m=(m-1)&b){
		mini(bit[m], d);
	  }
	}

	for(int b=0;b<(1<<K);++b){
	  for(int m=b;m>0;m=(m-1)&b){
		mini(bit[b], bit[b^m] + bit[m]);
	  }
	}

	int ans = INF;
	REP(b,1<<K) REP(t,H+1)
	  mini(ans, bit[b] + dist[b][T][t]);
	cout << (ans >= INF? -1: ans) << endl;
  }

  return 0;
}

Measured memory_bytes: 5772
N = int(input())
L = [0] * N
for i in range(N):
  s, p = map(str, input().split())
  p = -1 * int(p)
  L[i] = [s, p, i + 1]
L = sorted(L)
#print(L)
for i in range(N):
  print(L[i][2])


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

int cc(long long i)
{
    int ans=0;
    while(i)
    {
        if(i%2)
        ans++;
        i/=2;
    }
    return ans;
}
int main() {
	// your code goes here
int n;
cin>>n;
if(n<=999) cout<<"ABC"<<endl; 
else cout<<"ABD"<<endl;
return 0;
}

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

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

    if(A>=B){
        cout << "delicious";
    }else{
        int after = B-A;
        if(after <= X){
            cout << "safe";
        }else{
            cout << "dangerous";
        }
    }
    return 0;
}

Measured memory_bytes: 256
#coding; utf-8


def main():
    N = int(input())
    A = list(map(int, input().split()))
    min_color = 0
    max_color = 0
    rate_criteria = 400
    rate = [0]*9
    for i in range(N):
        diva = A[i]//rate_criteria
        if diva > 8:
            diva = 8
        rate[diva] += 1

    for i in range(8):
        if rate[i] > 0:
            min_color += 1

    if min_color == 0 and rate[-1] > 0:
        min_color = 1
        max_color = rate[-1]
    else:
        max_color = min_color + rate[-1]
    # print(rate)
    print(min_color, max_color)


if __name__ == "__main__":
    main()


Measured memory_bytes: 38256
def check_space(y,x):
    if g[y][x]==-1:
        return True
    return False

while 1:
    n=int(raw_input())
    if n==0:break
    g=[[-1]*n for _ in xrange(n)]
    for i in xrange(1,n**2+1):
        if i==1:
            g[n/2+1][n/2]="1"
            nowy=n/2+1
            nowx=n/2
            continue
        while 1:
            if check_space((nowy+1)%n,(nowx+1)%n):
                g[(nowy+1)%n][(nowx+1)%n]=str(i)
                nowy=(nowy+1)%n
                nowx=(nowx+1)%n
                break
            else:
                nowy=(nowy+1)%n
                nowx=(nowx-1)%n
                continue
    for i in xrange(n):
        tmp=""
        for j in xrange(n):
            tmp+=g[i][j].rjust(4)
        print(tmp)

Measured memory_bytes: 6284
#include <iostream>
#include <algorithm>
using namespace std;
int main(void){
    long long N,M,i,a,b,ans,mod;
    mod=1e9+7;ans=1;
    cin>>N>>M;
    long long A[N+1],B[M+1];
    for(i=0;i<N;i++){
        cin>>A[i];
    }
    for(i=0;i<M;i++){
        cin>>B[i];
    }
    A[N]=-1;B[M]=-1;
    sort(A,A+(N+1));
    sort(B,B+(M+1));
    a=N;b=M;
    for(i=0;i<N;i++){
        if(A[i]==A[i+1]){
            cout<<0<<endl;
            return 0;
        }
    }
    for(i=0;i<M;i++){
        if(B[i]==B[i+1]){
            cout<<0<<endl;
            return 0;
        }
    }
    while(a>0||b>0){
        if(A[a]>B[b]){
            ans=(ans*(M-b))%mod;
            a--;
        }else if(A[a]<B[b]){
            ans=(ans*(N-a))%mod;
            b--;
        }else{
            a--;b--;
        }
    }
    long long dig[N*M+1];
    for(i=0;i<N*M+1;i++){
        dig[i]=0;
    }
    for(i=0;i<N;i++){
        dig[A[i]]=1;
    }
    for(i=0;i<M;i++){
        dig[B[i]]=1;
    }
    long long nom[N*M+1];
    a=1;b=1;
    for(i=0;i<N*M+1;i++){
        while(A[a]<i){
            a++;
        }
        while(B[b]<i){
            b++;
        }
        nom[i]=(N-a+1)*(M-b+1)-(N*M-i);
    }
    for(i=N*M;i>0;i--){
        if(dig[i]==0){
            if(nom[i]<0){
                cout<<0<<endl;
                return 0;
            }else{
                ans=(ans*nom[i])%mod;
            }
        }
    }
    cout<<ans<<endl;
}


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

int main(){
    int a,b,d,r;
    double f;
    cin >> a >> b;
    
    d=a/b;
    r=a%b;
    f=(double)a/(double)b;
    
    cout << d <<" "<< r <<" "<<endl;
    printf("%5f",f);
    
    return 0;
    
}


Measured memory_bytes: 3220
#include <iostream>
#include <string>
#include <unordered_map>

using namespace std;
typedef long long ll;

int main(){
    ll n, m;
    unordered_map<string, ll> map;
    cin >> n;
    for(int i = 0; i < n; ++i){
        string s;
        cin >> s;
        map[s]++;
    }
    cin >> m;
    for(int i = 0; i < m; ++i){
        string s;
        cin >> s;
        map[s]--;
    }
    ll ans = 0;
    for(auto p : map){
        ans = max(ans, p.second);
    }
    cout << ans << endl;
    return 0;
}


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

using namespace std;

#define pb push_back
#define mp make_pair
constexpr int INF = 1 << 29;
constexpr int MOD = 1000000007;
typedef long long ll;
typedef unsigned long long ull;

constexpr int dx[4] = {1, 0, -1, 0};
constexpr int dy[4] = {0, 1, 0, -1};

int main() {
    int N, K;
    cin >> N >> K;
    
    cout << (N - 1 + (K - 2)) / (K - 1) << endl;
    
    return 0;
}


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

int main() {
    stack<int> s;
    string str;
    while (cin >> str) {
        if (str[0] == '+') {
            int t1 = s.top(); s.pop();
            int t2 = s.top(); s.pop();
            s.push(t1 + t2);
        }
        else if (str[0] == '-') {
            int t2 = s.top(); s.pop();
            int t1 = s.top(); s.pop();
            s.push(t1 - t2);
        }
        else if (str[0] == '*') {
            int t1 = s.top(); s.pop();
            int t2 = s.top(); s.pop();
            s.push(t1 * t2);
        }
        else {
            s.push(stoi(str));
        }
    }
    cout << s.top() << endl;
}

Measured memory_bytes: 3176