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

#define dump(x) cout << (x) << '\n'
#define Int int64_t
#define fi first
#define se second
#define all(x) (x).begin(), (x).end()

Int INF = 1e18;
int inf = 1e9;
Int mod = 1e9+7;

int d[100][100];

int main() {
    vector<int> res;
    while (true) {
        int h, w;
        cin >> w >> h;
        if (h == 0) break;
        map<pair<pair<int, int>, pair<int, int>>, int> mp;
        for (int i = 0; i < 2 * h - 1; i++) {
            if (i % 2 == 0) {
                for (int j = 0; j < w - 1; j++) {
                    int x;
                    cin >> x;
                    if (x == 1) {
                        mp[{{i / 2, j}, {i / 2, j + 1}}]++;
                        mp[{{i / 2, j + 1}, {i / 2, j}}]++;
                    }
                }
            }
            else {
                for (int j = 0; j < w; j++) {
                    int x;
                    cin >> x;
                    if (x == 1) {
                        mp[{{i / 2, j}, {i / 2 + 1, j}}]++;
                        mp[{{i / 2 + 1, j}, {i / 2, j}}]++;
                    }
                }
            }
        }
        for (int i = 0; i < h; i++) {
            for (int j = 0; j < w; j++) {
                d[i][j] = inf;
            }
        }
        d[0][0] = 1;
        queue<pair<int, int>> q;
        q.push({0, 0});
        while (not q.empty()) {
            pair<int, int> p = q.front();
            q.pop();
            int dx[] = {1, 0, -1, 0};
            int dy[] = {0, -1, 0, 1};
            for (int i = 0; i < 4; i++) {
                int ny = p.fi + dy[i];
                int nx = p.se + dx[i];
                if (ny < 0 or ny == h or nx < 0 or nx == w) continue;
                if (mp[{p, {ny, nx}}] > 0) continue;
                if (d[ny][nx] > d[p.fi][p.se] + 1) {
                    d[ny][nx] = d[p.fi][p.se] + 1;
                    q.push({ny, nx});
                }
            }
        }
        // for (int i = 0; i < h; i++) {
        //     for (int j = 0; j < w; j++) {
        //         cout << d[i][j] << " " ;
        //     }
        //     cout << endl;
        // }
        res.emplace_back(d[h - 1][w - 1] == inf ? 0 : d[h - 1][w - 1]);
    }
    for (int i = 0; i < (int)res.size(); i++) {
        dump(res[i]);
    }
    return 0;
}



Measured memory_bytes: 3304
from math import factorial
N = int(input())
S = list(input())
MOD = 10 ** 9 + 7

X = []
for i, s in enumerate(S):
    if i % 2 == 1:
        X.append(s)
    else:
        if s == 'W':
            X.append('B')
        else:
            X.append('W')


W_cnt = 0
ans = 1
for x in X:
    if x == 'W':
        W_cnt += 1
    else:
        ans *= W_cnt
        ans %= MOD
        W_cnt -= 1
else:
    if W_cnt:
        print(0)
    else:
        print(ans * factorial(N) % MOD)


Measured memory_bytes: 7332
#include <iostream>

using namespace std;

int main()
{
    int n,t,c,tt,mini=0;
    cin>>n>>t;
    int minn=100000;
    for(int i=0; i<n; i++){
        cin>>c>>tt;
        if(tt<=t){
            if(c<minn){
                minn=c;
                mini=i+1;
            }
        }
    }
    if(mini!=0)
        cout<<minn;
    else
        cout<<"TLE";
    return 0;
}


Measured memory_bytes: 256
#include<bits/stdc++.h>
#define rep(i,n) for (int i = 0; i < (n); ++i)
#define rrep(i,n) for (int i = (n)-1; i >= 0; i--)
#define rep2(i,s,n) for (int i = (s); i < (n); ++i)
#define all(a) a.begin(),a.end()
#define rall(a) a.rbegin(),a.rend()
#define pb push_back
#define eb emplace_back
#define vi vector<int>
#define vvi vector<vector<int>>
#define vl vector<ll>
#define vvl vector<vector<ll>>
#define vd vector<double>
#define vs vector<string>
#define vc vector<char>
#define vb vector<bool>
#define vp vector<P>
using namespace std;
using ll = long long;
using P = pair<int,int>;
using LP = pair<ll,ll>;
template<class S,class T> istream& operator>>(istream &is,pair<S,T> &p) { return is >> p.first >> p.second; }
template<class S,class T> ostream& operator<<(ostream &os,const pair<S,T> &p) { return os<<'{'<<p.first<<","<<p.second<<'}'; }
template<class T> istream& operator>>(istream &is,vector<T> &v) { for(T &t:v){is>>t;} return is; }
template<class T> ostream& operator<<(ostream &os,const vector<T> &v) { os<<'[';rep(i,v.size())os<<v[i]<<(i==v.size()-1?']':','); return os; }
void Yes(bool b) { cout << (b ? "Yes" : "No") << endl; }
void YES(bool b) { cout << (b ? "YES" : "NO") << endl; }
template<class T> bool chmin(T& a,T b) {if(a > b){a = b; return true;} return false;}
template<class T> bool chmax(T& a,T b) {if(a < b){a = b; return true;} return false;}
const int inf = 1001001001;
const ll linf = 1001001001001001001;

vi a;
int dp[100010];

int solve(int x) {
    if(dp[x] != -1) return dp[x];
    int ret = 1;
    for(int i : a) if(x >= i) chmin(ret,solve(x-i));
    return dp[x] = 1-ret;
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr); cout.tie(nullptr);
    int n,k;
    cin >> n >> k;
    a.resize(n);
    cin >> a;
    rep(i,100010) dp[i] = -1;
    cout << (solve(k) ? "First" : "Second") << endl;
}


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

int main(){
    int n;
    cin>>n;
    vector<int> a(n);
    for(int i=0; i<n-1; i++) {
        int x; cin>>x;
        a[x-1]++;
    }
    for(int i=0; i<n; i++) {
        cout<<a[i]<<"\n";
    }
}

Measured memory_bytes: 3980
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define repe(i, n) for (int i = 0; i <= (n); ++i)
#define all(x) (x).begin(),(x).end()
using namespace std;
template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return 1; } return 0; }
typedef long long ll;
const int INF = 1e9;
const ll mod = 1000000007;
int gcd(int a,int b){return b?gcd(b,a%b):a;}
int lcm(int a,int b){return a*b / gcd(a,b);}

int main() {
    int a,b;
    cin >> a >> b;
    cout << max(a+b,max(a-b,a*b)) << "\n";
}


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

#define INF 1000000007
#define LINF 1000000000000000007

using namespace std;

typedef long long Int;
typedef pair<Int, Int> P;

Int n, a[200010];

Int C(int c){
	Int x = 0;
	while(c){
		x += c;
		c--;
	}
	return x;
}

int main(){
	cin >> n;
	for(int i = 0; i < n; i++){
		cin >> a[i];
	}
	
	Int ans = 0, r = 0, cnt = 0;
	for(int i = 0; i < n; i++){
		if(i > 0) cnt -= a[i-1];
		while(r < n && cnt + a[r] == (cnt ^ a[r])){
			cnt += a[r];
			r++;
			ans += r-i;
		}
	}
	
	cout << ans << endl;
	
	return 0;
}

Measured memory_bytes: 1792
import java.util.Scanner;

public class Main{
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        
        int a = sc.nextInt();
        int b = sc.nextInt();
        int c = sc.nextInt();
        
        if((a<b)&&(b<c)){
            if(a<c){
                System.out.println("Yes");
            }else{
                System.out.println("No");
            }
        }else{
            System.out.println("No");
        }
    }
}


Measured memory_bytes: 26404
#include <iostream>
#include <iomanip>
#include <string>
#include <stack>
#include <vector>
#include <math.h>
#include <stdio.h>
#include <algorithm>
#include <utility>
#include <functional>
#include <map>
#include <set>
#include <queue>
#include <list>
#include <regex>
using namespace std;
using pii  = pair<int,int>;
using ll=long long;
using ld=long double;
#define pb push_back
#define mp make_pair
#define stpr setprecision
#define cYES cout<<"YES"<<endl
#define cNO cout<<"NO"<<endl
#define cYes cout<<"Yes"<<endl
#define cNo cout<<"No"<<endl
#define rep(i,n) for(ll i=0;i<(n);++i)
#define Rep(i,a,b) for(ll i=(a);i<(b);++i)
#define rrep(i,n) for(int i=n-1;i>=0;i--)
#define rRep(i,a,b) for(int i=a;i>=b;i--)
#define crep(i) for(char i='a';i<='z';++i)
#define psortsecond(A,N) sort(A,A+N,[](const pii &a, const pii &b){return a.second<b.second;});
#define ALL(x) (x).begin(),(x).end()
#define debug(v) cout<<#v<<":";for(auto x:v){cout<<x<<' ';}cout<<endl;
#define endl '\n'
int ctoi(const char c){
  if('0' <= c && c <= '9') return (c-'0');
  return -1;
}
ll gcd(ll a,ll b){return (b == 0 ? a : gcd(b, a%b));}
ll lcm(ll a,ll b){return a*b/gcd(a,b);}
constexpr ll MOD=1000000007;
constexpr ll INF=1000000011;
constexpr ll MOD2=998244353;
constexpr ll LINF = 1001002003004005006ll;
constexpr ld EPS=10e-8;
template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return 1; } return 0; }
template<typename T> istream& operator>>(istream& is,vector<T>& v){for(auto&& x:v)is >> x;return is;}
template<typename T,typename U> istream& operator>>(istream& is, pair<T,U>& p){ is >> p.first; is >> p.second; return is;}
template<typename T,typename U> ostream& operator>>(ostream& os, const pair<T,U>& p){ os << p.first << ' ' << p.second; return os;}
template<class T> ostream& operator<<(ostream& os, vector<T>& v){
    for(auto i=begin(v); i != end(v); ++i){
        if(i !=begin(v)) os << ' ';
        os << *i;
    }
    return os;
}

int main(){
    ll A,B,C,X[107];
    ll N,M;
    string S;
    cin  >> A>> B;

    cout << max(A-2*B,0LL) << endl;
}

Measured memory_bytes: 768
#include <sstream>
#include <string>
#include <vector>
#include <map>
#include <algorithm>
#include <iostream>
#include <utility>
#include <set>
#include <cctype>
#include <queue>
#include <stack>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>

using namespace std;

int main() {
	string s;
	string t;
	cin >> s;
	cin >> t;
	for (int i = 0; i < 3; i++) {
		if (s[i] != t[2 - i]) {
			cout << "NO" << endl;
			return 0;
		}
	}
	cout << "YES" << endl;
	return 0;
}



Measured memory_bytes: 256
#include <cstdio>
#include <cstdint>
#include <algorithm>
#include <vector>
#include <string>

int dsum(const std::string &s) {
  int res=0;
  for (char ch: s)
    res += ch-'0';

  return res;
}

int main() {
  char buf[20];
  scanf("%s", buf);

  std::string N=buf;

  int res=dsum(N);
  for (size_t i=N.size(); i-->1;) {
    N[i] = '9';
    --N[i-1];
    res = std::max(res, dsum(N));
  }

  printf("%d\n", res);
}


Measured memory_bytes: 256
main = do
  [x, y] <- map read . words <$> getLine
  putStr $ f x y
 
a = [1, 3, 5, 7, 8, 10, 12]
b = [4, 6, 9, 11]
c = [2]
 
f x y
  | elem x a && elem y a = "Yes"
  | elem x b && elem y b = "Yes"
  | elem x c && elem y c = "Yes"
  | otherwise = "No"

Measured memory_bytes: 508
#include<iostream>
#include<stdio.h>
#include<string>
#include<vector>
#include <cmath>
//#include <bits/stdc++.h>
using namespace std;

int main(){
    long long N,K,a;
    cin>>N>>K;
    a=N%K;
    cout<<min(a,K-a)<<endl;
    return 0;
}

Measured memory_bytes: 256
import numpy as np
N = int(input())
denom = 10**9+7

prime_counter = {}

def factorize(x):
    for i in range(2, int(N**0.5)+1):
        while (x%i == 0):
            if i not in prime_counter:
                prime_counter[i] = 1
            else:
                prime_counter[i] += 1
            x = x//i
    if x != 1:
        if x not in prime_counter:
            prime_counter[x] = 1
        else:
            prime_counter[x] += 1

for i in range(2,N+1):
    factorize(i)
order_list = list(prime_counter.values())
order_list = [i+1 for i in order_list]
res = 1
for i in range(len(order_list)):
    res = (res*order_list[i])%denom

print(res)

Measured memory_bytes: 12424
// IOI 2021
#include <bits/stdc++.h>
using namespace std;
 
#define endl '\n'
#define ends ' '
#define die(x) return cout << x << endl, 0
#define all(v) v.begin(), v.end()
#define sz(x) (int)(x.size())
void debug_out() { cerr << endl; }
template <typename Head, typename... Tail>
void debug_out(Head H, Tail... T) { cerr << ends << H; debug_out(T...); }
#define debug(...) cerr << "{" << #__VA_ARGS__ << "}:", debug_out(__VA_ARGS__)
typedef long long ll;
typedef pair<int, int> pii;
const int INF = 1e9;
const ll MOD = 1e9 + 7;
 
////////////////////////////////////////////////////////////////////
 
const int N = 1e5 + 5;

int G(int a, int k) {
	if (a < k) return 0;
	if (a % k == 0) return a / k;
	int r = a % k, d = a / k, t = (r + d) / (d + 1);
	return G(a - t * (d + 1), k);
}

int main() {
 
	ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
	mt19937 Rnd(time(0));


	int n, g = 0; cin >> n;
	for (int i = 0; i < n; i++) {
		int a, k; cin >> a >> k;
		g ^= G(a, k);
	}
	cout << (g ? "Takahashi" : "Aoki") << endl;

	return 0;
}


Measured memory_bytes: 3592
#include<iostream>
#include<string>
#include<algorithm>
#include<vector>
#include<queue>
#include<map>
#include<math.h>
#include<iomanip>
#include<set>
#include<numeric>
#include<cstring>
#include<cstdio>
#include<functional>

#define REP(i, n) for(int i = 0;i < n;i++)
#define REPR(i, n) for(int i = n;i >= 0;i--)
#define FOR(i, m, n) for(int i = m;i < n;i++)
#define FORR(i, m, n) for(int i = m;i >= n;i--)
#define SORT(v, n) sort(v, v+n);
#define VSORT(v) sort(v.begin(), v.end());
#define REVERSE(v,n) reverse(v,v+n);
#define VREVERSE(v) reverse(v.begin(), v.end());
#define ll long long
#define pb(a) push_back(a)
#define INF 999999999
#define m0(x) memset(x,0,sizeof(x))
#define fill(x,y) memset(x,y,sizeof(x))


using namespace std;


int dy[4] = { 0,0,1,-1 };
int dx[4] = { 1,-1,0,0 };
int dxx[8] = { 0,0,1,1,1,-1,-1,-1 };
int dyy[8] = { 1,-1,0,1,-1,0,1,-1 };




ll gcd(ll x, ll y) {
	ll m = max(x, y), n = min(x, y);
	if (m%n == 0)return n;
	else return gcd(m%n, n);
}
ll lcm(ll x, ll y) {
	return x / gcd(x, y)*y;
}

ll myPow(ll x, ll n, ll m) {
	if (n == 0)
		return 1;
	if (n % 2 == 0)
		return myPow(x * x % m, n / 2, m);
	else
		return x * myPow(x, n - 1, m) % m;
}

long long nCr(int n, int r) {
	if (r > n / 2) r = n - r; // because C(n, r) == C(n, n - r)
	long long ans = 1;
	int i;

	for (i = 1; i <= r; i++) {
		ans *= n - r + i;
		ans /= i;
	}

	return ans;
}


const int MOD = 1000000007;


bool alpha[26];
int main() {
	m0(alpha);
	string s;
	cin>>s;
	int N=s.length();
	REP(i,N){
		alpha[s[i]-'a']=true;
	}
	int ans=999999;
	REP(i,26){
		if(alpha[i]==true){
			int cnt=0;
			string str=s;
			bool stop=false;

			while(!stop){
				stop=true;
				string str2=str;
				REP(j,str2.length()){
					if(str2[j]!='a'+i)stop=false;
					if(j==str2.length()-1)continue;
					if(str[j]==(char)('a'+i)||str[j+1]==(char)('a'+i)){
						str2[j]='a'+i;
					}
					else{
						str2[j]=str[j];
						stop=false;
					}
				}
				if(stop)break;
				cnt++;
				str2.erase(str2.end()-1);
				
				str=str2;
			}
			ans=min(ans,cnt);
		}
	}
	cout<<ans<<endl;
}

Measured memory_bytes: 256
import sys
import time
#from collections import deque, Counter, defaultdict
#from fractions import gcd
#import bisect
#import heapq
import math
start=time.time()
input = sys.stdin.readline
sys.setrecursionlimit(10**8)
inf = 10**18
MOD = 1000000007
ri = lambda : int(input())
rs = lambda : input().strip()
rl = lambda : list(map(int, input().split()))
n=ri()
a=rl()
q=ri()
dp=[0]*(10**5+1)
bc=[]
for i in range(n):
    dp[a[i]]+=1
bc=[tuple(rl()) for i in range(q)]
ans=0
for i,j in enumerate(dp):
    ans+=i*j
  
for b,c in bc:
    ans+=-dp[b]*b+dp[b]*c
    print(ans)
    dp[c]+=dp[b]
    dp[b]=0
    


Measured memory_bytes: 89240
//***Blessed are the curious for they shall have adventures********

#include<bits/stdc++.h>
using namespace std;

#define mp make_pair
#define mt make_tuple
#define fi first
#define se second
#define pb push_back
#define ll long long
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define rep(i,n) for(i=0;i<n;i++)
#define forn(i, n) for (ll i = 0; i < (ll)(n); ++i)
#define for1(i, n) for (ll i = 1; i <= (ll)(n); ++i)
#define ford(i, n) for (ll i = (ll)(n) - 1; i >= 0; --i)
#define fore(i, a, b) for (ll i = (ll)(a); i <= (ll)(b); ++i)
#define fora(it,x) for(auto it:x)
#define PI 3.14159265
#define sync ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
#define endl "\n"

typedef pair<ll, ll> pii;
typedef vector<ll> vi;
typedef vector<pii> vpi;
typedef vector<vi> vvi;
typedef long long i64;
typedef vector<i64> vi64;
typedef vector<vi64> vvi64;
typedef pair<i64, i64> pi64;
typedef long double ld;

template<class T> bool uin(T &a, T b) { return a > b ? (a = b, true) : false; }
template<class T> bool uax(T &a, T b) { return a < b ? (a = b, true) : false; }

ll b[101];

int main()
{
    ll n,s=0;
    cin>>n;

    forn(i,n-1) cin>>b[i];
    s+=b[0]+b[n-2];

    for(ll i=1;i<(n-1);i++)
    s+=min(b[i],b[i-1]);
    cout<<s;

}

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

ll N;
vector<P> A;  //(活性度、番号)
vector<vector<ll>> dp(2010, vector<ll>(2010, 0));
ll res = 0;

int main(){
  cin >> N;

  ll a;
  for(ll i = 1; i <= N; i++){ cin >> a; A.push_back({a, i}); }

  sort(A.begin(), A.end());
  reverse(A.begin(), A.end());  //活性度大きい順

  for(int i = 0; i <= N; i++){  //活性度の高い順かつ合計
      ll ac = A[i].first, n = A[i].second;  //活性度とナンバー
      for(int j = 0; j <= i; j++){
        int k = i - j;
        dp[j+1][k] = max(dp[j+1][k], dp[j][k] + ac * abs(j+1-n));
        dp[j][k+1] = max(dp[j][k+1], dp[j][k] + ac * abs(N-k-n));
    }
  }

  for(int i = 0; i <= N; i++) res = max(res, dp[i][N-i]);
  cout << res << endl;
  return 0;
}


  





Measured memory_bytes: 35200
S = input()
T = input()
flag = True
sdict = {}
tdict = {}
for si, ti in zip(S, T):
    if si not in sdict:
        sdict[si] = len(sdict)
    if ti not in tdict:
        tdict[ti] = len(tdict)
    if sdict[si] == tdict[ti]:
        continue
    flag = False
    break

if flag:
    print('Yes')
else:
    print('No')


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

using ll = long long;

#define reps(i,s,n) for(int (i) = (s); (i) < (n); (i)++)
#define rep(i,n) reps(i,0,n)

const int MAXV = 200001;
int main()
{
	int n,m;
	cin >> n >> m;
	vector<int> graph[MAXV];
//	vector<edge> graph[MAXV];
	int a,b;

	rep(i,m){
		cin >> a >> b;
		a--;b--;
		graph[a].push_back(b);
	}

	int y = 0;
	rep(i,graph[0].size()){
		y = graph[0][i];
		rep(j,graph[y].size()){
			if(graph[y][j] == n-1){
				cout << "POSSIBLE" << endl;				
				return 0;
			}
		}
	}
	cout << "IMPOSSIBLE" << endl;
	
	return 0;
}


Measured memory_bytes: 8576
#include<bits/stdc++.h>
using namespace std;
const int size = 1e5 + 5;
int p[size], q[size], r[size];
int x, y, a, b, c;
long long ans;
int main() {
	cin >> x >> y >> a >> b >> c;
	for(int i = 0; i < a; i++)	cin >> p[i];
	for(int i = 0; i < b; i++)	cin >> q[i];
	for(int i = 0; i < c; i++)	cin >> r[i];
	sort(p, p + a);
	sort(q, q + b);
	sort(r, r + c);
	p[a] = INT_MAX;
	q[b] = INT_MAX;
	int i = a - x, j = b - y, k = c - 1;
	while(i <= a && j <= b && k >= 0) {
		if(p[i] <= q[j] && p[i] < r[k]) {
			ans += r[k--];
			i++;
		}
		else if(q[j] < p[i] && q[j] < r[k]) {
			ans += r[k--];
			j++;
		}
		else	break;
	}
	while(i < a)	ans += p[i++];
	while(j < b)	ans += q[j++];
	cout << ans << "\n";
	return 0;
}

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

int main()
{
  int a, b;
  int res=0;
  cin >> a >> b;

  for(int i=1;i<=a;i++){
    if(i==a && b < a) break;
    res++;
  }

  cout << res << endl;
}

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

using namespace std;

int arr[100][100];

int main() {
    cin.tie(0);
    ios::sync_with_stdio(0);
    
    string line;
    int pos = 0;
    while(cin >> line) {
        for(int i = 0; i < (int)line.size(); ++i) {
            if(line[i] == ',') line[i] = ' ';
        }
        istringstream is(line);
        for(int i = 0; !is.eof(); ++i) {
            is >> arr[pos][i];
        }
        ++pos;
    }
    const int N = pos;
    const int center = N / 2;
    vector<int> dp[2];
    dp[0].resize(100);
    dp[1].resize(100);
    dp[0][0] = arr[0][0];
    for(int i = 0; i < center; ++i) {
        fill(dp[1].begin(), dp[1].end(), 0);
        for(int j = 0; j <= i; ++j) {
            // left
            dp[1][j] = max(dp[1][j], dp[0][j] + arr[i+1][j]);
            // right
            dp[1][j+1] = max(dp[1][j+1], dp[0][j] + arr[i+1][j+1]);
        }
        dp[0].swap(dp[1]);
    }
    for(int i = center; i < N-1; ++i) {
        fill(dp[1].begin(), dp[1].end(), 0);
        for(int j = 0; j < N-i; ++j) {
            // left
            if(j > 0) dp[1][j-1] = max(dp[1][j-1], dp[0][j] + arr[i+1][j-1]);
            // right
            dp[1][j] = max(dp[1][j], dp[0][j] + arr[i+1][j]);
        }
        dp[0].swap(dp[1]);
    }
    cout << dp[0][0] << endl;
    return 0;
}

Measured memory_bytes: 1268
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        try (Scanner sc = new Scanner(System.in)) {
            while (true) {
                int n = sc.nextInt();
                if (n == 0) {break;}
                int[] scores = new int[n];
                for (int i = 0; i < scores.length; i++) {
                    scores[i] = sc.nextInt();
                }
                double average = 0, variance = 0, sum = 0;
                for(int j = 0; j < n; j++){
                    sum += scores[j];
                }
                average = sum/n;
                for(int k = 0; k < n; k++){
                    variance += (scores[k]-average)*(scores[k]-average)/n;
                }

                System.out.println(Math.sqrt(variance));
            }
        }
    }
}


Measured memory_bytes: 31488
INT_MAX=10**11+7
MOD=10**9+7
def INPUT():return list(int(i) for i in input().split())
def LIST_1D_ARRAY(n):return [0 for _ in range(n)]
def LIST_2D_ARRAY(m,n):return [[0 for _ in range(n)]for _ in range(m)]
#=======================================================================
import math as M
n=int(input())
bool=True
A=INPUT()
for i in range(n):
    if A[i]%2==0:
        if A[i]%3==0 or A[i]%5==0:
            continue
        bool=False
        break
if bool:
    print("APPROVED")
else:
    print("DENIED")


Measured memory_bytes: 38256
import sys
#from collections import defaultdict, deque, Counter
#import bisect
#import heapq
#import math
#from itertools import accumulate
#from itertools import permutations as perm
#from itertools import combinations as comb
#from itertools import combinations_with_replacement as combr
#from fractions import gcd
#import numpy as np

stdin = sys.stdin
sys.setrecursionlimit(10 ** 7)
MIN = -10 ** 9
MOD = 10 ** 9 + 7
INF = float("inf")
IINF = 10 ** 18

def main():
    #n = int(stdin.readline().rstrip())
    x,y = map(int, stdin.readline().rstrip().split())
    #x = list(map(int, stdin.readline().rstrip().split()))
    #numbers = [[int(c) for c in l.strip().split()] for l in sys.stdin]
    #word = [stdin.readline().rstrip() for _ in range(n)]
    #number = [[int(c) for c in stdin.readline().rstrip()] for _ in range(n)]
    #zeros = [[0] * w for i in range(h)]
    for b in range(x+1):
        a = x - b
        if 4 * b + 2*a == y:
            print("Yes")
            exit()
    print("No")

main()


Measured memory_bytes: 9188
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Main{

    static int[] height;
    static int[] minPossible;
    static int size;

    public static void main(String[] args) throws IOException {

        BufferedReader inp = new BufferedReader(new InputStreamReader(System.in));
        String[] sz = inp.readLine().split(" ");
        size = Integer.parseInt(sz[0]);
        int jump = Integer.parseInt(sz[1]);

        String[] given = inp.readLine().split(" ");
        height = new int[size];
        minPossible = new int[size];

        for(int i=0;i<size;i++){
            height[i] = Integer.parseInt(given[i]);
        }


        ans(jump);
        System.out.println(minPossible[size-1]);



    }

    static void ans(int jump){

        minPossible[0] = 0;
        int minValue = 1147483647;

        for(int i=1;i<size;i++){

            minValue = 1147483647;

            for (int j=1;j<=jump;j++){
                if(i-j>=0) {
                    int a = height[i];
                    int b = height[i - j];

                    int c = a - b;
                    if(c<0){
                        c = -c;
                    }

                    //System.out.println("c"+c);

                    int d = minPossible[i - j];
                    int value = d + c;

                    if (value < minValue) {
                        minValue = value;
                    }
                }
            }

            minPossible[i] = minValue;
        }
    }
}


Measured memory_bytes: 34648
import sys
sys.setrecursionlimit(10**6)

N = int(input())

tree = {}
for i in range(1,N):
    u, v, w = map(int, input().split())
    w = w%2
    if u in tree:
        tree[u].append([v,w])
    else:
        tree[u] = [[v,w]]

    if v in tree:
        tree[v].append([u,w])
    else:
        tree[v] = [[u,w]]

iro = [-1]*(N)
def seachTree(current, parent):
    if len(tree[current]) == 0:
        return

    for elms in tree[current]:
        if elms[0] == parent:
            continue
        if elms[1] == 0:
            iro[elms[0]-1] = iro[current-1]
        else:
            iro[elms[0]-1] = not iro[current-1]

        seachTree(elms[0], current)

iro[0] = False
seachTree(1, 0)
for val in iro:
    print(int(val))

Measured memory_bytes: 160088
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define UNIQUE(v) v.erase( unique(v.begin(), v.end()), v.end() );

using namespace std;
int main(){
    long long n,ans=0;
    cin >> n;
    rep(i,n+1){
        if(i%3 != 0 && i%5 != 0){
            ans += i;
        }
    }
    cout << ans << endl;
}


Measured memory_bytes: 3632
#include <iostream>
#include <sstream>
#include <string>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <numeric>
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cctype>
#include <cmath>
#include <cassert>
#include <cstdarg>
#include <sys/time.h>
#include <fstream>
//#include "cout.h"

using namespace std;

#define SZ(x) ((int)x.size())
#define MSET(x,a) memset(x, a, (int)sizeof(x))
#define PB push_back
#define VI vector < int >
#define PII pair < int, int >
#define LL long long
#define FOR(i,a,b) for (int i = (a); i < (b); i++)
#define REP(i,n) FOR(i,0,n)
#define ALL(v) (v).begin(), (v).end()
#define FIT(it,v) for (__typeof((v).begin()) it = (v).begin(); it != (v).end(); it++)
#define OUT(A) cout << #A << " = "<< (A) << endl
#define OUT2(A, B) cout << "(" << #A << ", " << #B << ") = (" << (A) << ", "<< (B) << ")" << endl
template<class T> void chmin(T &t, T f) { if (t > f) t = f; }
template<class T> void chmax(T &t, T f) { if (t < f) t = f; }

#define present(c, e) ((c).find((e)) != (c).end())
#define cpresent(c, e) (find(ALL(c), (e)) != (c).end())

set<LL> s;
vector<pair<int, int> > vp;
VI v;
LL k, n;

bool pairCompare(pair<int, int> a, pair<int, int> b){
    return a.second > b.second;
}

void init() {

}

void input() {
  cin >> n >> k ;
  REP(i, n) {
    LL a, b;
    cin >> a >> b;
    vp.push_back(make_pair(a, b));
  }
}

void solve() {
  sort(vp.begin(), vp.end(), pairCompare);
  LL sum = 0;
  REP(i, k) {
    if (s.count(vp[i].first)) v.push_back(vp[i].second);
    s.insert(vp[i].first);
    sum += vp[i].second;
  }
  LL ret = sum + s.size()*s.size();
  sort(ALL(v));
  int ptr = k;
  REP(i, v.size()) {
    while(ptr < n) {
      if (s.count(vp[ptr].first) == 0) {
        s.insert(vp[ptr].first);
        sum -= v[i];
        sum += vp[ptr].second;
        chmax(ret, sum + (LL)(s.size()*s.size()));
        ptr++;
        break;
      }
      ptr++;
    }
  }
  cout << ret << endl;
}

int main() {
	init();
	input();
	solve();
	return 0;
}


Measured memory_bytes: 4212
N,K=map(int,input().split())
ls=list(map(int,input().split()))
for _ in range(K):
  if ls==[N]*N:
    break
  else:   
    R=[0]*(N)
    for i in range(N):
      R[max([0,i-ls[i]])]+=1
      if i+ls[i]+1<N:
        R[i+ls[i]+1]-=1  
    ls[0]=R[0]
    for i in range(N-1):
      ls[i+1]=ls[i]+R[i+1]   
for i in range(N):
  print(ls[i],end=" ")  

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

using namespace std;

int main() {
    int n = 0;
    long long ans = 0;
    cin >> n;

    for (int i = 1; i <= n; ++i) {
        if (i % 3 != 0 && i % 5 != 0)ans += i;
    }
    cout << ans << endl;
}


Measured memory_bytes: 3640
import java.util.*;

public class Main {

  public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    int a = sc.nextInt();
    String op = sc.next();
    int b = sc.nextInt();

    if (op.equals("+")) {
      System.out.println(a+b);
    } else {
      System.out.println(a-b);
    }
  }

}


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

using namespace std;

typedef long long ll;

int main()
{

    int n, m;
    cin >> n >> m;

    vector<string> a(n), b(m);

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

    for (int i = 0; i + m <= n; i++)
    {
        for (int j = 0; j + m <= n; j++)
        {
            bool ok = true;
            for (int ii = 0; ii < m; ii++)
            {
                for (int jj = 0; jj < m; jj++)
                {
                    if (a[i + ii][j + jj] != b[ii][jj])
                    {
                        ok = false;
                    }
                }
            }
            if (ok)
            {
                cout << "Yes" << endl;
                return 0;
            }
        }
    }
    cout << "No" << endl;
    return 0;
}

Measured memory_bytes: 256
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <climits>
#include <cfloat>
#include <map>
#include <utility>
#include <set>
#include <iostream>
#include <memory>
#include <string>
#include <vector>
#include <algorithm>
#include <functional>
#include <sstream>
#include <complex>
#include <stack>
#include <queue>
#include <cstring>
#include <iomanip>
#include <cassert>

using namespace std;
typedef long long ll;
// auto mod int
// https://youtu.be/L8grWxBlIZ4?t=9858
// https://youtu.be/ERZuLAxZffQ?t=4807 : optimize
// https://youtu.be/8uowVvQ_-Mo?t=1329 : division
const int mod = 1000000007;
struct mint
{
   ll x; // typedef long long ll;
   mint(ll x = 0) : x((x % mod + mod) % mod) {}
   mint operator-() const { return mint(-x); }
   mint &operator+=(const mint a)
   {
      if ((x += a.x) >= mod)
         x -= mod;
      return *this;
   }
   mint &operator-=(const mint a)
   {
      if ((x += mod - a.x) >= mod)
         x -= mod;
      return *this;
   }
   mint &operator*=(const mint a)
   {
      (x *= a.x) %= mod;
      return *this;
   }
   mint operator+(const mint a) const { return mint(*this) += a; }
   mint operator-(const mint a) const { return mint(*this) -= a; }
   mint operator*(const mint a) const { return mint(*this) *= a; }
   mint pow(ll t) const
   {
      if (!t)
         return 1;
      mint a = pow(t >> 1);
      a *= a;
      if (t & 1)
         a *= *this;
      return a;
   }

   // for prime mod
   mint inv() const { return pow(mod - 2); }
   mint &operator/=(const mint a) { return *this *= a.inv(); }
   mint operator/(const mint a) const { return mint(*this) /= a; }
};
istream &operator>>(istream &is, const mint &a) { return is >> a.x; }
ostream &operator<<(ostream &os, const mint &a) { return os << a.x; }
int main()
{
   int N;
   cin >> N;
   vector<int> A(N);
   for (int i = 0; i < N; i++)
   {
      cin >> A[i];
   }
   mint ans = 0;
   mint sum = 0;
   for (int i = N - 1; i >= 0; i--)
   {
      ans += sum * A[i];
      sum += A[i];
   }
   cout << ans << endl;
   return 0;
}


Measured memory_bytes: 4044
#include <iostream>
#include <unordered_set>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;

int main(void) {
  int N;
  cin >> N;
  unordered_set<int> mochi;
  rep(i, N) {
    int d;
    cin >> d;
    mochi.emplace(d);
  }
  cout << mochi.size() << endl;
  return 0;
}

Measured memory_bytes: 256
#include<bits/stdc++.h>
using namespace std;
int main(){
  int n,k,r,s,p;
  cin>>n>>k>>r>>s>>p;
  map<char,int> h;
  h['r']=p;
  h['s']=r;
  h['p']=s;
  string t;
  cin>>t;
  int w=0;
  for(int i=0;i<k;i++)
    w+=h.at(t.at(i));
  vector<bool> d(k);
  for(int i=k;i<n;i++){
    if(t.at(i)==t.at(i-k)){
      if(d.at(i%k))
        w+=h.at(t.at(i));
      d.at(i%k)=!d.at(i%k);
    }
    else{
      w+=h.at(t.at(i));
      d.at(i%k)=0;
    }
  }
  cout<<w<<endl;
}

Measured memory_bytes: 512
//
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using vi = vector<int>;
using vvi = vector<vi>;

#define MOD 1000000007
#define rp(i,n) for(int i=0;i<(n);i++)
#define irp(i,n) for(int i=(n);i>=0;i--)
#define up(i,s,e,d) for(int i=(s);i<(e);i+=(d))
#define dw(i,s,e,d) for(int i=(s);i>(e);i-=(d))
// #define in(x) cin>>(x)
#define ot(x) cout<<(x)
#define ote(x) cout<<(x)<<endl
#define MAX(a,b) ((a)>(b)?(a):(b))
#define MIN(a,b) ((a)<(b)?(a):(b))
#define vecmax(v) (*max_element(v.begin(),v.end()))
#define range(v,x) (iota((v).begin(),(v).end(),(x)))
void bye(auto x, int code=0){cout<<(x)<<endl;exit(code);}
// int in(){int x;cin>>x;return x;}
int in(){int x;scanf("%d",&x);return x;}
// template<class T> T in(){T x;cin>>x;return x;}

void vprn(vector<int> &v){ for(int i=0;i<v.size();i++) cout<<v[i]<<(i<v.size()-1?",\t":"\n"); }
void vvprn(vector<vector<int>> &vv){ for(auto v:vv) vprn(v); }

// template<class T> void vprn(T &v, string s, string m, string e)
// {cout<<s;for(int i=0;i<v.size();i++)cout<<v[i]<<m;cout<<e;}
// template<class T> void vvprn(T &vv, string s, string m, string e)
// {for(auto v:vv) vprn(v,s,m,e);}

int main(){
	int n,m;
	cin>>n>>m;
	vvi a(n+1,vi(2));
	up(i,1,n+1,1){
		cin>>a[i][0]>>a[i][1];
	}
	vvi b(m+1,vi(2));
	up(i,1,m+1,1){
		cin>>b[i][0]>>b[i][1];
	}

	// up(i,1,n+1,1){cout<<a[i][0]<<","<<a[i][1]<<"\n";}
	// up(i,1,m+1,1){cout<<b[i][0]<<","<<b[i][1]<<"\n";}

	vi ans(n+1,1e9);
	up(i,1,n+1,1){
		// int mini = 1000000000;
		int mini = 1e9;
		up(j,1,m+1,1){
			int c  = abs(a[i][0]-b[j][0]);
			c  += abs(a[i][1]-b[j][1]);
			// cout<<i<<","<<j<<", ";ote(c);//
			if(mini>c){
				mini = c;
				ans[i] = j;
				// ans[i] = min(ans[i], j);
			}
		}
		// ote("^^^^^^^^^");
	}

	up(i,1,n+1,1){
		cout<<ans[i]<<"\n";
	}
}

Measured memory_bytes: 256
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<cctype>
#include<iostream>
#include<string>
#include<sstream>
#include<set>
#include<map>
#include<vector>
#include<algorithm>
#include<queue>
#include<utility>
using namespace std;

struct modular_arithmetic{
	int mod;

	modular_arithmetic(){
		mod = 1000000007;
	}
	//No specific settings.
	int add(int x,int y){return (x+=y)<mod?x:x-mod;}
	int sub(int x,int y){return (x-=y)>=0?x:x+mod;}
	int mul(int x,int y){return (int)((long long int)x*y%mod);}
	int pw(int base, int p) {
		int res = 1, mu = base%mod;
		for(;p>0;p/=2) {
			if (p&1) res = mul(res, mu);
			mu = mul(mu, mu);
		}
		return res;
	}
	int rev(int x) {return pw(x, mod-2);}
};
modular_arithmetic ma;
void addto(int&x,int y){x=ma.add(x,y);}

#define ran 5050
int n;
int f[ran], sz[ran];
int dp[ran][ran][2], tmp[ran][2];
vector<int> e[ran];
void dfs(int x,int fat){
	dp[x][1][0] = 1;
	sz[x] = 1;
	for(auto y : e[x]){
		if(y == fat)continue;
		dfs(y, x);
		memset(tmp,0,sizeof(tmp));
		for(int i=1; i<=sz[x]; i++)for(int r=0; r<2; r++)if(dp[x][i][r])
			for(int j=1; j<=sz[y]; j++)for(int r2=0; r2<2; r2++){
				int w = ma.mul(dp[x][i][r], dp[y][j][r2]);
				addto(tmp[i][r^r2^1], ma.mul(w, f[j]));
				addto(tmp[i+j][r^r2], w);
			}
		memcpy(dp[x],tmp,sizeof(tmp));
		sz[x] += sz[y];
	}
}

int main() {
	scanf("%d", &n);
	f[0] = 1; f[1] = 0;
	for(int i=2; i<=n; i++)f[i] = ma.mul(f[i-2], i-1);
	for(int i=1; i<n; i++){
		int x,y;
		scanf("%d%d",&x,&y);
		e[x].push_back(y);
		e[y].push_back(x);
	}
	dfs(1, 1);
	int res = 0;
	for(int i=1; i<=n; i++){
		res = ma.add(res, ma.mul(f[i], ma.sub(dp[1][i][0], dp[1][i][1])));
	}
	printf("%d\n",res);
	return 0;
}

Measured memory_bytes: 199808
lines = $stdin.read
array = lines.split("\n")

N,M = array[0].split(" ").map(&:to_i)
A = array[1].split(" ").map(&:to_i)
sums = 0
nums = {}

nums[0] = 1
A.each.with_index do |e,i|
  sums += e
  # count mods
  if nums[sums % M].nil?
    nums[sums % M] = 1
  else
    nums[sums % M] += 1
  end
end

#p A
#p nums

ans = 0
nums.each do |k,v|
  ans += v*(v-1)/2
end

puts ans

Measured memory_bytes: 12668
#include <iostream>
#include <vector>
#include <algorithm>
#include <cstdio>

using namespace std;

typedef pair<double, double> P2;
typedef pair<string, P2> P;

int main() {
	vector<P> st;
	st.push_back(  P("AAA", P2(35.50, 71.00) )   );
	st.push_back(  P("AA",  P2(37.50, 77.00) )   );
	st.push_back(  P("A",   P2(40.00, 83.00) )   );
	st.push_back(  P("B",   P2(43.00, 89.00) )   );
	st.push_back(  P("C",   P2(50.00, 105.00) )  );
	st.push_back(  P("D",   P2(55.00, 116.00) )  );
	st.push_back(  P("E",   P2(70.00, 148.00) )  );

	double a, b;
	while (scanf("%lf%lf", &a, &b) != EOF) {
		bool f = false;
		for (int i = 0; i < st.size(); ++i) {
			if (a < st[i].second.first && b < st[i].second.second) {
				cout << st[i].first << endl;
				f = true;
				break;
			}
		}
		if (!f) cout << "NA" << endl;
	}
}

Measured memory_bytes: 1244
#include <bits/stdc++.h>
using namespace std;
 
#define REP(i, s) for (int i = 0; i < s; ++i)
#define ALL(v) (v).begin(), (v).end()
#define COUT(x) cout << #x << " = " << (x) << " (L" << __LINE__ << ")" << endl
#define EACH(i, s) for (__typeof__((s).begin()) i = (s).begin(); i != (s).end(); ++i)
#define DEBUG
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; }
template<class T1, class T2> ostream& operator << (ostream &s, pair<T1,T2> P)
{ return s << '<' << P.first << ", " << P.second << '>'; }
template<class T> ostream& operator << (ostream &s, vector<T> P)
{ for (int i = 0; i < P.size(); ++i) { if (i > 0) { s << " "; } s << P[i]; } return s; }
template<class T> ostream& operator << (ostream &s, vector<vector<T> > P)
{ for (int i = 0; i < P.size(); ++i) { s << endl << P[i]; } return s << endl; }
template<class T> ostream& operator << (ostream &s, set<T> P)
{ EACH(it, P) { s << "<" << *it << "> "; } return s << endl; }
template<class T1, class T2> ostream& operator << (ostream &s, map<T1,T2> P)
{ EACH(it, P) { s << "<" << it->first << "->" << it->second << "> "; } return s << endl; }
template<class T>void show(vector<T>v){for (int i = 0; i < v.size(); i++){cerr<<v[i]<<" ";}cerr<<"\n";}
typedef long long ll;

int s[100010],t[100010],c[100010];
int v[40][100010];
int main(){
    int N,C;
    cin>>N>>C;
    for (int i = 0; i < N; i++)
    {
        cin>>s[i]>>t[i]>>c[i];
        s[i]--;
    }
    for (int i = 0; i < N; i++)
    {
        
        v[c[i]][s[i]]++;
        v[c[i]][t[i]]--;
    }
    for (int i = 1; i <=30; i++)
    {
        for (int j = 1; j <=100000; j++)
        {
            v[i][j]+=v[i][j-1];
        }
        
    }
  /* for (int i = 0; i < 15; i++)
   {
       cerr<<v[1][i];
   }*/
   ll min_c=0;
   ll cnt=0;
   for (int j = 0; j <= 100000; j++)
   {    cnt=0;
       for (int i = 1; i <= 30; i++)
       {
           if(v[i][j])
           cnt+=1;
       }
       if(cnt>=min_c){
           min_c=cnt;
       }
       
   }
   cout<<min_c<<endl;
   
   
    
    


    return 0;
}

Measured memory_bytes: 14592
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <cmath>
#include <vector>
#include <set>
#include <map>
#include <unordered_set>
#include <unordered_map>
#include <queue>
#include <ctime>
#include <cassert>
#include <complex>
#include <string>
#include <cstring>
#include <chrono>
#include <random>
#include <queue>
#include <bitset>
#include <stack>
#include <functional>

#define rep(i, n) for(ll i = 0, i##_len = (n); i < i##_len; ++i)
#define repp(i, m, n) for(ll i = m, i##_len = (n); i < i##_len; ++i)
#define reprev(i, n) for(ll i = (n-1LL); i >= 0; --i)
#define all(x) (x).begin(), (x).end()
template <class T> bool chmax(T &a, const T &b) { if (a < b) { a = b; return 1; } return 0; }
template <class T> bool chmin(T &a, const T &b) { if (b < a) { a = b; return 1; } return 0; }
template <class T> T gcd(T a, T b) { return b ? gcd(b, a % b) : a; }
using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair <int,int> P;
typedef long double ld;

int main(void)
{
	cin.tie(0);
	ios::sync_with_stdio(false);
	int n;
	cin >> n;
	vector<int> a(n);
	ll ans = 1000000000000000LL;
	rep (i, n) cin >> a[i];
	
	rep (x, 2) {
		ll now = 0, tmp = 0;
		rep (i, n) {
			now += a[i];
			if ((i % 2 == x) && now <= 0) {
				tmp += (1 - now);
				now = 1;
			} else if ((i % 2 != x) && now >= 0) {
				tmp += (now - (-1));
				now = -1;
			}
		}
		chmin(ans, tmp);
	}
	cout << ans << "\n";

	return 0;
}

Measured memory_bytes: 640
# ABC 118 D

def resolve():
    N, M = map(int, input().split())
    A = list(map(int, input().split()))
    A.sort(reverse=True)

    costs = [0, 2, 5, 5, 4, 5, 6, 3, 7, 6]

    dp = [-1] * (N+1)
    dp[0] = 0
    for i in range(1, N+1):
        for a in A:
            if i - costs[a] < 0:
                continue
            dp[i] = max(dp[i-costs[a]] + 1, dp[i])

    ans = ""
    remain = dp[N]
    match = N

    minCosts = 10
    for a in A:
        minCosts = min(minCosts, costs[a])

    while match > 0:
        for a in A:
            if match - costs[a] < 0 or 1<= match - costs[a] < minCosts:
                continue

            if dp[match-costs[a]] == remain-1:
                ans += str(a)
                match -= costs[a]
                remain -= 1
                break

    print(ans)


if __name__ == "__main__":
    resolve()


Measured memory_bytes: 3388
#[allow(unused_imports)]
use std::cmp::*;
#[allow(unused_imports)]
use std::collections::*;
use std::io::{Write, BufWriter};
// https://qiita.com/tanakh/items/0ba42c7ca36cd29d0ac8
macro_rules! input {
    ($($r:tt)*) => {
        let stdin = std::io::stdin();
        let mut bytes = std::io::Read::bytes(std::io::BufReader::new(stdin.lock()));
        let mut next = move || -> String{
            bytes
                .by_ref()
                .map(|r|r.unwrap() as char)
                .skip_while(|c|c.is_whitespace())
                .take_while(|c|!c.is_whitespace())
                .collect()
        };
        input_inner!{next, $($r)*}
    };
}

macro_rules! input_inner {
    ($next:expr) => {};
    ($next:expr, ) => {};

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

macro_rules! read_value {
    ($next:expr, ( $($t:tt),* )) => {
        ( $(read_value!($next, $t)),* )
    };

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

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

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

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

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

#[allow(unused)]
macro_rules! debug {
    ($($format:tt)*) => (write!(std::io::stderr(), $($format)*).unwrap());
}
#[allow(unused)]
macro_rules! debugln {
    ($($format:tt)*) => (writeln!(std::io::stderr(), $($format)*).unwrap());
}

fn solve() {
    let out = std::io::stdout();
    let mut out = BufWriter::new(out.lock());
    macro_rules! puts {
        ($($format:tt)*) => (write!(out,$($format)*).unwrap());
    }
    input! {
        n: usize,
        d: [i64; n],
    }
    let mut tot = 0;
    for i in 0..n {
        for j in 0..i {
            tot += d[i] * d[j];
        }
    }
    puts!("{}\n", tot);
}

fn main() {
    // In order to avoid potential stack overflow, spawn a new thread.
    let stack_size = 104_857_600; // 100 MB
    let thd = std::thread::Builder::new().stack_size(stack_size);
    thd.spawn(|| solve()).unwrap().join().unwrap();
}


Measured memory_bytes: 8572
from sys import stdin
input = stdin.readline

ans = 10 ** 10

N, M, X = map(int, input().strip().split())

C = []
A = [[]] * N

for i in range(N):
    tmp = list(map(int, input().strip().split()))
    C.append(tmp[0])
    A[i] = tmp[1:]

# for i in range(2 ** N - 1):
for i in range(2 ** N):
    tans = 0
    tA = [0] * M
    for j in range(N):
        if i & (1 << j):
            tans += C[j]
            for k in range(M):
                tA[k] += A[j][k]

    flag = True
    for k in range(M):
        flag &= (tA[k] >= X)

    #print(i, tA, tans)

    if flag:
        ans = min(ans, tans)

if ans == 10 ** 10:
    print(-1)
else:
    print(ans)


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

int n, m, amax, bmin, x, y;

int main(){
	cin >> n >> m >> x >> y;
	amax = -110;
	bmin = 110;
	int tmp;
	for (int i=1; i<=n; ++i){
		cin >> tmp;
		amax = max(amax, tmp);
	}
	for (int i=1; i<=m; ++i){
		cin >> tmp;
		bmin = min (bmin, tmp);
	}
	for (int i=x+1; i<=y; ++i){
		if (i>amax && i<=bmin){
			cout << "No War";
			return 0;
		}
	}
	cout << "War";
	return 0;
}

Measured memory_bytes: 256
#pragma GCC optimize("O3")
#include<bits/stdc++.h> 
using namespace std;
using ll=long long;
using ull=unsigned long long int;
using P=pair<ll,ll>;
template<class T> using V=vector<T>; 
#define fi first
#define se second
#define all(v) (v).begin(),(v).end()
const ll inf=(1e18);
const ll mod=1000000007;
ll gcd(ll a,ll b) {return b ? gcd(b,a%b):a;}
ll lcm(ll c,ll d){return c/gcd(c,d)*d;}
struct __INIT{__INIT(){cin.tie(0);ios::sync_with_stdio(false);cout<<fixed<<setprecision(15);}} __init;
template<class T> bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T> bool chmin(T &a, const T &b) { if (a>b) { a=b; return 1; } return 0; }
int main(){
while(1){
  string s;
  cin>>s;
  if(s=="#")return 0;
  reverse(all(s));
  int m=s.size();
  int n=0,w=0;
  ll ans=0;
  int cnt=0;
  for(int i=0;i<m;i++){
    if(s[i]=='h'){
      if(i!=0){
        ans*=2;
        ans--;
      }
      i+=4;
      n++;
    }else{
      ans*=2;
      ans++;
      w++;
      i+=3;
    }
    cnt++;
  }
  ans*=90;
  ll sum=(1ll<<(cnt-1));
  ll d=gcd(ans,sum);
  ans/=d;
  sum/=d;
  if(sum==1)cout<<ans<<"\n";
  else cout<<ans<<'/'<<sum<<"\n";
}
}


Measured memory_bytes: 3192
#include <bits/stdc++.h>
using namespace std;
#define repr(i,a,b) for(int i=a;i<b;i++)
#define rep(i,n) for(int i=0;i<n;i++)
#define reprrev(i,a,b) for(int i=b-1;i>=a;i--) // [a, b)
#define reprev(i,n) reprrev(i,0,n)
typedef long long ll;
typedef unsigned long long ull;
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; }

const ll mod = 1e9+7;

void chmod(ll &M){
    if(M >= mod) M %= mod;
    else if(M < 0){
        M += (abs(M)/mod + 1)*mod;
        M %= mod;
    }
}

ll modpow(ll x, ll n){
    if(n==0) return 1;
    ll res=modpow(x, n/2);
    if(n%2==0) return res*res%mod;
    else return res*res%mod*x%mod;
}

ll power(ll x, ll n){
    if(n == 0) return 1;

    ll res = power(x, n/2);
    if(n%2 == 0) return res*res;
    else return res*res*x;
}

int getl(int i, int N) { return i==0? N-1:i-1; };
int getr(int i, int N) { return i==N-1? 0:i+1; };

// 線分 ab の偏角 返り値は[-π, π]
double argument(const pair<double, double> &a, const pair<double, double> &b){
    double ax=a.first, ay=a.second, bx=b.first, by=b.second;
    return atan2(by-ay, bx-ax);
}

/* <-----------------------------------------------------------------------------------> */
/* <-----------------------------------------------------------------------------------> */
/* <-----------------------------------------------------------------------------------> */
/* <-----------------------------------------------------------------------------------> */


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

    int N; cin >> N;
    vector<ll> A(N);
    rep(i, N) cin >> A[i];

    vector<ll> bitCnt(65, 0);

    ll ans = 0;
    rep(i, N) {
        rep(j, 60) {
            if(A[i]&(1LL<<j)) { ans += (1LL<<j) % mod * (i - bitCnt[j]); bitCnt[j]++; }
            else { ans += (1LL<<j) % mod * bitCnt[j]; }
            chmod(ans);
        }
    }

    cout << ans << endl;

    return 0;
}


Measured memory_bytes: 2560