akhauriyash/Code-Regression huggingface dataset:
#include <bits/stdc++.h>
#define dbug(x) cout<<#x<<"="<<x<<endl
using namespace std;
template <typename T> void read(T &t) {
	t=0; char ch=getchar(); int f=1;
	while ('0'>ch||ch>'9') { if (ch=='-') f=-1; ch=getchar(); }
	do {(t*=10)+=ch-'0';ch=getchar();} while ('0'<=ch&&ch<='9'); t*=f;
}
typedef long long ll;
const ll mod=(1e9)+7;
const ll M=998244353;
const int maxn=(2e5)+10;
int n;
char s[maxn];
int main() {
	//freopen("1.txt","r",stdin);
	scanf("%s",s+1);
	n=strlen(s+1);
	int pos=1,lst=0,ans=0;
	while (pos<=n) {
		if (pos==1) ans++,pos++;
		else if (lst==2) ans++,pos++,lst=1;
		else if (s[pos]==s[pos-1]) {
			if (pos==n) break;
			ans++,pos+=2,lst=2;
		} else ans++,pos++,lst=1;
	}
	printf("%d\n",ans);
	return 0;
}

Measured memory_bytes: 384
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <numeric>
#include <string>
#include <sstream>
#include <complex>
#include <vector>
#include <array>
#include <list>
#include <queue>
#include <deque>
#include <stack>
#include <map>
#include <set>
using namespace std;
typedef long long unsigned int ll;

#define EPS (1e-7)
#define INF (1e9)
#define PI (acos(-1))

int gcd(const int& a, const int& b){
   const int& _a = max(a,b);
   const int& _b = min(a,b);
   if(_a % _b == 0){
      return _b;
   }
   else{
      return gcd(_b, _a % _b);
   }
}

int lcm(const int& a, const int& b){
   const int& _a = max(a,b);
   const int& _b = min(a,b);
   return (_a*_b)/(gcd(_a, _b));
}

int main(){
   int N;
   cin >> N;
   for(int i=0; i <= N/4; i++){
      for(int j=0; j <= N/7; j++){
         if(4*i + 7*j == N){
            cout << "Yes" << endl;
            return 0;
         }
      }
   }
   cout << "No" << endl;

   return 0;
}

Measured memory_bytes: 256
#include <bits/stdc++.h>
using namespace std;
int main(void){
    int n,x,y,z;
    cin>>n;
    vector<int> c,s,f;
    for(int i=0;i<n-1;i++){
        cin>>x>>y>>z;
        c.push_back(x);
        s.push_back(y);
        f.push_back(z);
    }
    for(int i=0;i<n;i++){{
        int ans=0;
        int loc=i;
        while(loc<n-1){
            for(int t=ans;1;t++){
                if(t>=s[loc]&&t%f[loc]==0){
                    ans=t+c[loc];
                    loc++;
                    break;
                }
            }
        }
        cout<<ans<<endl;
        }
    }
}


Measured memory_bytes: 256
import std.stdio;
import std.string;
import std.conv;
import std.math;

int main()
{
	double x=0.0,y=0.0,angle=90;

	while(true)
	{
		string[] s = readln.chomp.split(",");
		if(s[0] == "0" && s[1] == "0") break;

		int value = s[0].to!int;
		int value_2 = s[1].to!int;

		x += cos(angle*PI/180)*value;
		y += sin(angle*PI/180)*value;
		angle -= value_2;
	}
	writeln(cast(int)x);
	writeln(cast(int)y);

	return 0;
}

Measured memory_bytes: 1064
#include <bits/stdc++.h>
#define rep(i,n) for(int i=0;i<(n);i++)
using namespace std;
typedef long long ll;
int main(){
  int n;
  cin>>n;
  int v[n];
  int c[n];
  rep(i,n){
    cin>>v[i];
  }
  rep(i,n){
    cin>>c[i];
  }
  int ans=0;
  rep(i,n){
    int sell=v[i]-c[i];
    if(sell>=0){
    ans+=sell;
    }
  }
  cout<<ans<<endl;
}

Measured memory_bytes: 256
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for(int i = 0, i##_len = (n); i < i##_len; ++i)
#define rep2(i, x, n) for(int i = x, i##_len = (n); i < i##_len; ++i)
#define all(n) begin(n), end(n)
using ll = long long;
using P = pair<ll, ll>;
using vi = vector<int>;
using vl = vector<ll>;
using vs = vector<string>;
using vc = vector<char>;
using vb = vector<bool>;
using vd = vector<double>;
vi dir = {-1, 0, 1, 0, -1, -1, 1, 1, -1};

template <typename T>
struct BIT {
  int n;
  vector<T> d;
  BIT(int n = 0) : n(n), d(n + 1) {}
  void add(int i, T x = 1) {
    for(i++; i <= n; i += i & -i) {
      d[i] += x;
    }
  }
  T sum(int i) {
    T x = 0;
    for(i++; i; i -= i & -i) {
      x += d[i];
    }
    return x;
  }
};

int main() {
  ll n, k;
  cin >> n >> k;
  vl s(n + 1, 0);
  rep(i, n) {
    ll a;
    cin >> a;
    a -= k;
    s[i + 1] = s[i] + a;
  }
  vl ss = s;
  sort(all(ss));
  ss.erase(unique(all(ss)), ss.end());
  BIT<ll> b(n + 1);
  ll ans = 0;
  rep(i, n + 1) {
    ll in = lower_bound(all(ss), s[i]) - ss.begin();
    ans += b.sum(in);
    b.add(in);
  }
  cout << ans << endl;
}

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

int main(){
    int n;
    cin>>n;
    set<string>s;
    for(int i = 0; i < n; i++){
        string str;
        cin>>str;
        s.insert(str);
    }
    cout<<s.size()<<endl;
    return 0;
}

Measured memory_bytes: 19120
#include<bits/stdc++.h>
#define st first
#define nd second
#define pb push_back
#define pf push_front
#define ppb pop_back
#define ppf pop_front
#define umax(x,y) x=max(x,y)
#define umin(x,y) x=min(x,y)
#define ll long long 
#define ii pair<int,int>
#define iii pair<ii,int>
#define iiii pair<ii,ii>
#define sz(x) ((int) x.size())
#define orta ((bas+son)/2)
#define all(x) x.begin(),x.end()
#define pw(x) (1<<(x))
#define inf 1000000000
#define MOD 1000000007 
#define N 200005
#define M 1000003
#define MAX 2000000
#define LOG 20
#define KOK 300
#define EPS 0.0000001
using namespace std;

int main() {

	int n;

	cin>>n;

	vector<string> v(n);

	for(int i=0;i<n;i++) cin>>v[i];

	int ans=0;
	int a=0,b=0,ba=0;

	for(int i=0;i<n;i++) {

		for(int j=0;j<sz(v[i])-1;j++) {

			ans+=(v[i][j]=='A' && v[i][j+1]=='B');

		}

		if(v[i][0]=='B') {

			if(v[i].back()=='A') ba++;
			else b++;

		}
		else if(v[i].back()=='A') a++;

	}

	printf("%d",ans+min(a,b)+max(0,ba-(!a && !b)));

} 

Measured memory_bytes: 768
#include<bits/stdc++.h>
using namespace std; 
deque<int> next_good(deque<int> s) {
    int p = s.size() - 1;
    while (p >= 0 && s[p] == 9) {
        s[p] = 0;
        p--;
    }
    if (p == -1) {
        s.push_front(1);
    } else {
        s[p]++;
    }
    while (true) {
        int b = -1;
        for(int i=0;i<s.size()-1;i++)
		{
            if (abs(s[i] - s[i + 1]) > 1) {
                b = i;
                break;
            }
        }
        if (b == -1) {
            break;
        }
        if (s[b + 1] > s[b]) {
            s[b]++;
        }
        int lll = max(s[b] - 1, 0);
        for(int i=b+1;i<s.size();i++){
            s[i] = lll;
            lll = max(lll - 1, 0);
        }
    }
    return s;
}
deque<int> ans;
int main()
{

    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.precision(20);
    cout << fixed;
	ans.clear();
    int k;
    cin >> k;
   
    for(int i=0;i<k;i++) {
        ans = next_good(ans);
    }
    for (int i : ans) {
        cout << i;
    }
    cout << "\n";
    return 0;
}
 

Measured memory_bytes: 256
import java.util.*;

public class Main {

	public static void main(String[] args){
		Scanner sc = new Scanner(System.in);
		String S = sc.next();
		int countB = 0;
		long answer = 0;
		for (int i = 0; i < S.length(); i++) {
			if (S.charAt(i) == 'B') {
				countB++;
			} else {
				answer += countB;
			}
		}
		System.out.println(answer);
	}
}

Measured memory_bytes: 26552
#include <iostream>
#include <string>
#include <vector>
#include <array>
#include <queue>
#include <stack>
#include <algorithm>
#include <limits>
#include <cassert>

#define seq(i, n) for (int i = 0; i < (n); i++)
#define seq2(i, f, t) for (int i = (f); i < (t); i++)
#define rev(i, n) for (int i = (n); i >= 0; i--)
#define rev2(i, f, t) for (int i = (f); i >= (t); i--)
#define all(x) (x).begin(),(x).end()
#define max(x, y) ((x) > (y) ? (x) : (y))
#define min(x, y) ((x) < (y) ? (x) : (y))

using namespace std;

using ll = long long;

int solve(int n, vector<int> v) {
  int t[100000]={0};
  for(int a:v){
    t[a]++;
  }
  int b=0;
  int res=0;
  const int n2=n/2;
  for(int a:t){
    b+=a;
    if(b==n2){
      res++;
    }
  }
  return res;
}

int main() {
#ifdef TEST
  assert(solve(6,{9,1,4,4,6,7})==2); 
  assert(solve(8,{9,1,14,5,5,4,4,14})==0); 
  assert(solve(14,{99592,10342,29105,78532,83018,11639,92015,77204,30914,21912,34519,80835,100000,1})==42685);
#else
  int n;
  cin >> n;
  vector<int> v;
  seq(i, n){
    int d;
    scanf("%d", &d);
    v.push_back(d);
  }
  cout << solve(n, v) << endl;
#endif

  return 0;
}


Measured memory_bytes: 1528
h, w, k = map(int, input().split())

c = []
nb = 0
for _ in range(h):
   tmp = [x for x in input()]
   nb += tmp.count('#')
   c.append(tmp)

d = []
for i in range(w):
   tmp = []
   for j in range(h):
      tmp.append(c[j][i])
   d.append(tmp)

x = [0] * h
for i in range(h):
   x[i] = c[i].count('#')

ans = 0
for i in range(2**h):
   y = [0] * w
   for j in range(w):
      y[j] = d[j].count('#')

   cnt1 = 0
   for ik in range(h):
      if ((i >> ik) & 1):
         cnt1 += x[ik]
         for j in range(w):
            if c[ik][j] == '#':
               y[j] -= 1

   for j in range(2**w):
      cnt2 = 0
      for jk in range(w):
         if ((j >> jk) & 1):
            cnt2 += y[jk]

      if(nb - (cnt1 + cnt2)) == k:
         ans += 1

print(ans)


Measured memory_bytes: 9124
#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>
using namespace std;
#define INF 100000000
#define MAXN 4096
typedef long long ll;
bool done[MAXN];
vector<int> syain;
int main(void) {
    int n;
    while (1) {
        cin >> n;
        int flag = 0;
        if (n == 0) break;
        map<ll, ll> m;
        m.clear();
        syain.clear();
        for (int i = 0; i < MAXN; i++) done[i] = false;
        for (int i = 0; i < n; i++) {
            ll bango, tanka, num;
            cin >> bango >> tanka >> num;
            if (!done[bango]) {
                done[bango] = true;
                syain.push_back(bango);
            }
            m[bango] += (m[bango] >= 1000000) ? 0 : tanka * num;
        }
        for (int i = 0; i < syain.size(); i++) {
            if (m[syain[i]] >= 1000000) {
                cout << syain[i] << endl;
                flag = 1;
            }
        }
        if (flag == 0) cout << "NA" << endl;
    }
    return 0;
}

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

int main(){
  long n; cin >> n;
  vector<vector<int>> a(n, vector<int>(60, 0));
  for(int i = 0; i < n; i++){
    long long A; cin >> A;
    for(int j = 0; j < 60; j++){
      if((A >> j) & 1) a.at(i).at(j) = 1;
    }      
  }
  vector<long> out(60);
  for(int i = 0; i < 60; i++){
    int sum = 0;
    for(int j = 0; j < n; j++){
      if(a.at(j).at(i) == 1) sum++;
    }
    out.at(i) = sum;
  }
  vector<long long> out2(60);
  for(int i = 0; i < 60; i++){
    out2.at(i) = out.at(i) * (n - out.at(i));
  }
  long long res = 0;
  long long two = 1;
  for(int i = 0; i < 60; i++){
    res += ((out2.at(i) % 1000000007) * (two % 1000000007)) % 1000000007;
    two *= 2;
    two = two;
  }
  cout << res % 1000000007;
}


Measured memory_bytes: 82304
#include <iostream>
#include <vector>
#include <cstdlib>
#include <algorithm>
#include <cmath>
#include <limits>
#include <unordered_map>

using namespace std;

int main() {

	int n;string k; cin >> n; cin >> k;

	unordered_map<string, pair<bool,bool>> alf{};

	for (auto& s : "abcdefghijklmnopqrstuvwxyz"){
		string a{s};
		alf[a] = make_pair(false,false);
	}

	int maxCount = 0;
	for (int i = 1; i < k.size(); i++){
		string a = k.substr(0, i);
		string b = k.substr(i);
		int count = 0;

		for (auto& aa : a){
			string aaa{aa};
			if (!alf.at(aaa).first){
				alf[aaa].first = true;
			}
		}

		for (auto& bb : b){
			string bbb{bb};
			if (alf[bbb].first && !alf[bbb].second){
				alf[bbb].second = true;
				count++;
			}
		}

		if (maxCount < count) maxCount = count;

		for (auto& p : alf){
			p.second.first = false;
			p.second.second = false;
		}
	}

	cout << maxCount;

}


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

///--->>> CONTAINERS <<<---///
using ll = long long;
using ld = long double;
using vi = vector<ll>;
using vvi = vector<vector< ll > >;
using vpi = vector<pair<ll,ll> >;
using pi = pair<ll,ll>;
using st = set<ll>;
using stk = stack<ll>;
using kiwi = queue<ll>;
using pkiwi = priority_queue<ll>;
using mp = map<ll,ll>;
using ump = unordered_map<ll,ll>;

///--->>> MACROS<<<---///
const ll mx = 3e5+123;
#define MOD 1000000007
#define REP(i,n) for (ll i=1;i<=n;++i)
#define fastio ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);

///--->>>OPTIMIZE<<<---///
#pragma GCC target ("avx2")
#pragma GCC optimization ("O3")
#pragma GCC optimization ("unroll-loops")

///--->>> DEBUGGER<<<---///
#define dbg(args...) do {cerr<<#args<<" : "; diraf(args); } while(0)
void diraf ()  {        cerr << endl;}
template < typename T, typename ... hello>void diraf( T arg, const hello &... rest) {cerr<<arg<< ' ';diraf(rest...);}

int main(){
    fastio;
    ///cout<<fixed<<setprecision(20);
    ll a, b, c, d; cin>>a>>b>>c>>d;
    ll x1 = a * c, x2 = a* d, x3 = b*c, x4 = b*d;
    ll ans=max(x1, max(x2, max(x3, x4)));
cout<<ans<<endl;

return 0;
}


Measured memory_bytes: 3676
#include<cstdio>
const int MAXN=4005,MOD=1000000007;

int N,M,K;
int dp[MAXN][MAXN],sum[MAXN][MAXN];

int main()
{
	scanf("%d%d%d",&N,&M,&K);
	dp[0][0]=1;
	for(int i=1;i<N+M;i++)
		for(int j=0;j<=M;j++)
		{
			sum[i][j]=((j>0?sum[i][j-1]:0)+dp[i-1][j])%MOD;
			dp[i][j]=(sum[i][j]-(j-K>=0?sum[i][j-K]:0)+MOD)%MOD;
		}
	int ans=0;
	while(M>0)
	{
		ans=(ans+dp[(N+M-1)/(K-1)][M])%MOD;
		M-=K-1;
	}
	printf("%d\n",ans);
	
	return 0;
}


Measured memory_bytes: 124800
// C++ 14
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define loop(__x, __start, __end) for(int __x = __start; __x < __end; __x++)
//  cout.precision(15); cout << fixed;

int main() {
  int n, d; cin >> n >> d;
  vector<vector<int> > X(n);
  loop(i,0,n) {
    loop(j,0,d) {
      int x; cin >> x;
      X[i].push_back(x);
    }
  }
  
  int total = 0;
  loop(i,0,n-1) {
    loop(j,i+1,n) {
      int dist = 0;
      loop(k,0,d) {
        dist += (X[j][k]-X[i][k])*(X[j][k]-X[i][k]);
      }
      bool ok = false;
      loop(x,0,401) {
        ok |= x*x == dist;
      }
      total += ok;
    }
  }
  cout << total << endl;
  return 0;
}


Measured memory_bytes: 3640
#include <iostream>
#include <vector>
#include <algorithm>
#include <set>
#include <queue>
#include <stack>
#include <string>
#include <map>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>

#define rep(i, n) for(int i = 0; i < n; i ++)
#define COUNT(i, n) for(int i = 1; i <= n; i ++)
#define ALL(T) T.begin(), T.end()
#define mp make_pair
#define pb push_back
#define ITER(c) __typeof((c).begin())
#define each(c, it) for(ITER(c) it = (c).begin(); it != (c).end(); it++)
#define X first
#define Y second

using namespace std;

typedef pair<int, int> pii;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef vector<string> vs;

const int INF = 1 << 24;
const int N = 1000001;

int main(void){
    for(int e, m; cin >> e, e; cout << m << endl){
        m = INF;
        rep(z, 101)
        rep(y, 1001){
            int x = e - z * z * z - y * y;
            if(x >= 0) m = min(m, x + y + z);
        }
    }

    return 0;
}

Measured memory_bytes: 1160
#include <bits/stdc++.h>
//#include <ext/pb_ds/assoc_container.hpp> // Common file 
//#include <ext/pb_ds/tree_policy.hpp> 
#define int long long
#define pb push_back
#define ff first
#define ss second
#define ii insert
#define ld long double
#define sz(x) (int)x.size()
#define all(v) v.begin(),v.end()
#define ppb pop_back
#define nuenxfiu ios_base::sync_with_stdio(0);cin.tie(NULL);cout.tie(NULL);
#define input freopen("input.txt", "r", stdin),freopen("output.txt", "w", stdout);
#define TT int testcases;cin>>testcases;while(testcases--)
#define mk make_pair
#define MAX ((int)2e9+17)
#define mod ((int)1e9+7)
#define MOD 1000003
//int fact[1000007]={0};
#define N 1e5+5
int fpow(int n, int k, int p = mod) {int r = 1; while(k > 0) {if (k & 1) r = r * n%p; n = n * n%p; k = k >> 1;} return r;}
int expo(int x,int y){ int res=1; while(y) {if(y%2) res=(res*x%mod)%mod; x=(x*x)%mod; y/=2; } return res;}
int sub(int a, int b) {return (a%mod - b%mod + mod)%mod;}
int mul(int a, int b) {return ((a%mod)*(b%mod) + mod)%mod;}
int inv(int x) {return expo(x, mod-2);}
using namespace std;

int a[100005];
int dp[100005];
int n;
int k;
int func(int id)
{
	if(id==n)	return 0;
	
	if(dp[id]!=-1) return dp[id];
	
	int mini=1e18;
	
	for(int i=id+1;i<=min(n,id+k);i++)
	{
		mini=min(mini,abs(a[i]-a[id])+func(i));
	}
	
	return dp[id]=mini;

}

void solve()
{
	cin>>n;
	cin>>k;
	
	for(int i=1;i<=n;i++)
		cin>>a[i];
	
	memset(dp,-1,sizeof(dp));
	
	cout<<func(1)<<endl;;
	
	return ;

}
signed main()
{
    
	nuenxfiu
	//TT
	solve();
     return 0;
}

Measured memory_bytes: 9788
#include<bits/stdc++.h>
const int MN=100000+5;
using namespace std;
typedef long long ll;
template<typename T>inline T&IN(T&in){
	in=0;char c=getchar();int f=1;
	while(!isdigit(c)){if(c=='-')f=-1;c=getchar();}
	while(isdigit(c))in=in*10+c-'0',c=getchar();
	return in*=f;
}
int n,m;
ll a[MN],b[MN],c[MN];
void input(){
	IN(n),IN(m);
	if(!m){
		for(int i=1;i<=n;++i)printf("1\n");
		return ;
	}
	for(ll i=1,x;i<=m;++i){
		IN(x);
		while(a[0]&&a[a[0]]>=x)a[0]--;
		a[++a[0]]=x;
	}
	m=a[0];b[m]=1;a[0]=min((ll)n,a[1]);
	for(int i=m;i>=1;--i){
		int p=i-1;
		while(a[i]>a[0]){
			b[p]+=a[i]/a[p]*b[i];
			a[i]%=a[p];
			p=upper_bound(a+1,a+p+1,a[i])-a-1;
		}
		c[a[i]]+=b[i];
	}
	c[a[0]]+=b[0];
	for(int i=n;i>=1;--i)c[i]+=c[i+1];
	for(int i=1;i<=n;++i)printf("%lld\n",c[i]);
}
int main(){
	input();
	return 0;
}

Measured memory_bytes: 4096
def main():
    N, L = (int(i) for i in input().split())
    s = 0
    li = []
    for i in range(1, N+1):
        s += L + i - 1
        li.append((abs(L + i - 1), i))
    li.sort()

    print(s - (L + li[0][1] - 1))


if __name__ == '__main__':
    main()


Measured memory_bytes: 38384
#include <iostream>
#include <iomanip>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <climits>
#include <algorithm>
#include <numeric>
#include <utility>
#include <random>
#include <chrono>
#include <string>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <unordered_set>
#include <unordered_map>

using namespace std;

using LL = long long;
using ULL = unsigned long long;
using PII = pair<int, int>;
using PIL = pair<int, LL>;
using PLL = pair<LL, LL>;
const int mod = 1e9 + 7;
// const int mod = 998244353;

inline void quickread() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
}

struct Matrix {
    int** g;
    int N;

    Matrix(int _N) {
        N = _N;
        g = new int*[N];
        for (int i = 0; i < N; ++i) {
            g[i] = new int[N];
            for (int j = 0; j < N; ++j) {
                g[i][j] = (i == j);
            }
        }
    }

    Matrix(const Matrix& that) {
        N = that.N;
        g = new int*[N];
        for (int i = 0; i < N; ++i) {
            g[i] = new int[N];
            for (int j = 0; j < N; ++j) {
                g[i][j] = that.g[i][j];
            }
        }
    }
    
    Matrix(Matrix&& that) {
        N = that.N;
        g = that.g;
    }

    ~Matrix() {
        for (int i = 0; i < N; ++i) {
            delete[] g[i];
        }
        delete[] g;
    }

    Matrix& operator= (const Matrix& that) {
        if (this != &that) {
            for (int i = 0; i < N; ++i) {
                for (int j = 0; j < N; ++j) {
                    g[i][j] = that.g[i][j];
                }
            }
        }
        return *this;
    }

    Matrix& operator*= (const Matrix& that) {
        int tmp[N][N];
        memset(tmp, 0, sizeof(tmp));
        for (int i = 0; i < N; ++i) {
            for (int j = 0; j < N; ++j) {
                for (int k = 0; k < N; ++k) {
                    tmp[i][j] += (LL)g[i][k] * that.g[k][j] % mod;
                    tmp[i][j] %= mod;
                }
            }
        }
        for (int i = 0; i < N; ++i) {
            for (int j = 0; j < N; ++j) {
                g[i][j] = tmp[i][j];
            }
        }
        return *this;
    }
};

int n;
LL k;

Matrix quickmul(const Matrix& g, LL y) {
    Matrix ret(n), mul = g;
    while (y) {
        if (y & 1) {
            ret *= mul;
        }
        mul *= mul;
        y >>= 1;
    }
    return ret;
}

inline void work() {
    cin >> n >> k;
    Matrix m(n);
    for (int i = 0; i < n; ++i) {
        for (int j = 0; j < n; ++j) {
            cin >> m.g[i][j];
        }
    }
    m = quickmul(m, k);
    int ans = 0;
    for (int i = 0; i < n; ++i) {
        for (int j = 0; j < n; ++j) {
            ans += m.g[i][j];
            ans %= mod;
        }
    }
    cout << ans << "\n";
}

int main() {
    // freopen("input.txt", "r", stdin);
    quickread();
    work();
    return 0;
}

Measured memory_bytes: 256
import java.util.Scanner;

public class Main {

	public static void main(String[] args) {
		
		try (Scanner sc = new Scanner(System.in);) {
			int labels1[] = new int[6];
			int labels2[] = new int[6];
			
			for(int i = 0; i < 6; i++) {
				labels1[i] = sc.nextInt();
			}
			for(int i = 0; i < 6; i++) {
				labels2[i] = sc.nextInt();
			}
			
			Dice dice1 = new Dice(labels1);
			Dice dice2 = new Dice(labels2);
			
			int count1 = 0;
			while(dice1.getSideS() != dice2.getSideS()) {
				if(count1++ == 4) {
					dice2.move("E");
				}
				dice2.move("S");
			}
			
			int count2 = 0;
			while(dice1.getTop() != dice2.getTop()) {
				if(count2++ >= 4) {
					System.out.println("No");
					return;
				}
				dice2.move("E");
			}
			
			if(dice1.getSideE() != dice2.getSideE()) {
				System.out.println("No");
				return;
			}
			
			System.out.println("Yes");
		}
	}
}

class Dice {
	
	private int position[];
	
	Dice(int labels[]){
		this.position = labels;
	}
	
	void move(String order) {
		if(order.equals("N")) {
			position = new int[]{position[1], position[5], position[2], position[3], position[0], position[4]};
			return;
		}
		if(order.equals("E")) {
			position = new int[]{position[3], position[1], position[0], position[5], position[4], position[2]};
			return;
		}
		if(order.equals("S")) {
			position = new int[]{position[4], position[0], position[2], position[3], position[5], position[1]};
			return;
		}
		if(order.equals("W")) {
			position = new int[]{position[2], position[1], position[5], position[0], position[4], position[3]};
			return;
		}
	}
	
	int getTop() {
		return position[0];
	}
	
	int getSideS() {
		return position[1];
	}
	
	int getSideE() {
		return position[2];
	}
}


Measured memory_bytes: 26320
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <cstdio>
#include <vector>
#include <string>
#include <array>
#include <queue>
#include <deque>
#include <set>
#include <list>
#include <map>
#include <stack>
#include <utility>
#include <algorithm>
#include <numeric>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cctype>
#include <cstring>
#include <climits>
#include <bitset>
#include <random>
#include <functional>
#include <sstream>
#include <iomanip>

using namespace std;

#define rep(i, n) for(int i=0; i<(n); i++)
#define FOR(i, m, n) for(int i=(m);i<(n);i++)
#define sz(x) ((int)(x).size())
#define all(x) (x).begin(),(x).end()
#define SORT(x) sort((x).begin(),(x).end())
#define REVE(x) reverse((x).begin(),(x).end())
#define mp make_pair
#define pb push_back
#define F first
#define S second


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

int main() {
	int n, q;
	cin >> n;
	VI a(n); rep(i, n)cin >> a[i];
	cin >> q;
	rep(i, q) {
		int b, e;
		cin >> b >> e;
		reverse(a.begin() + b, a.begin() + e);
	}
	rep(i, sz(a)) {
		cout << a[i];
		printf(i == sz(a) - 1 ? "\n" : " ");
	}
}


Measured memory_bytes: 3236
#include <stdio.h>

int main(void){

  char s[5];
  scanf("%s", s);
  int count = 0;
  for (int i = 0; i < 4; i++) {
    if (s[i] == '2') {
      count++;
    }
  }
  printf("%d\n", count);

  return 0;
}

Measured memory_bytes: 128
#include<iostream>
#include<bits/stdc++.h>
using namespace std;
#define vec vector<lld>
#define maxn 200007
#define lld long long 
#define endl '\n'
#define pb push_back
#define mod1 1000000007
#define mod 998244353
#define rep(i,a,b) for(lld i=a;i<b;i++)

lld max(lld x,lld y)
{
	if(x>y)
		return x;
	return y;
}	

vector<lld>g[maxn];
lld visited[maxn];
lld dp[maxn];



void foo(lld i)
{
	visited[i]=1;

	for(auto x:g[i])
	{
		if(visited[x] == 0)
		{
			foo(x);
		}

		dp[i]=max(dp[i],1+dp[x]);
	}

}

void solve()
{
	lld n,m,x,y;
	cin>>n>>m;
	memset(dp,0,sizeof(dp));
	memset(visited,0,sizeof(visited));
	rep(i,0,m)
	{
		cin>>x>>y;
		g[x].push_back(y);
	}
	rep(i,1,n+1)
	{
		if(visited[i]==0)
		{
			foo(i);
		}
	}
	
	lld mx=0;
	rep(i,1,n+1)
	{
		mx=max(mx,dp[i]);
	}
	cout<<mx<<endl;

}

int main()
{
	ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
	int t=1;
	//cin>>t;
	while(t--)
	{
		solve();
	}
}



Measured memory_bytes: 13312
#include <iostream>
#include <vector>
#include <queue>
#include <utility>

using namespace std;

inline bool check(const vector<int> &icicles, int pos) {
    const int N = (int)icicles.size();
    if(pos < 0 || pos >= N) return false;
    if(icicles[pos] == 0) return false;
    bool left = pos>0 ? icicles[pos-1] < icicles[pos] : true;
    bool right = pos+1<N ? icicles[pos+1] < icicles[pos] : true;
    return left && right;
}

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

    int N, L;
    cin >> N >> L;
    vector<int> icicles(N);
    for(int i = 0; i < N; ++i) {
        cin >> icicles[i];
    }
    int ans = 0;
    priority_queue<pair<int,int> > q;
    for(int i = 0; i < N; ++i) {
        if(check(icicles, i)) {
            int t = L - icicles[i];
            q.push(make_pair(-t, i));
        }
    }
    while(!q.empty()) {
        int t = -q.top().first;
        int pos = q.top().second;
        q.pop();
        ans = t;

        icicles[pos] = 0;
        if(check(icicles, pos-1)) {
            int nt = t + L - icicles[pos-1];
            q.push(make_pair(-nt, pos-1));
        }
        if(check(icicles, pos+1)) {
            int nt = t + L - icicles[pos+1];
            q.push(make_pair(-nt, pos+1));
        }
    }
    cout << ans << endl;
    return 0;
}

Measured memory_bytes: 1708
#include <bits/stdc++.h>
using namespace std;
#define rep(i,N) for(int i=0,i##_max=(N);i<i##_max;++i)
#define repp(i,l,r) for(int i=(l),i##_max=(r);i<i##_max;++i)
#define per(i,N) for(int i=(N)-1;i>=0;--i)
#define perr(i,l,r) for(int i=r-1,i##_min(l);i>=i##_min;--i)
#define all(arr) (arr).begin(), (arr).end()
#define SP << " " <<
#define SPF << " "
#define SPEEDUP cin.tie(0);ios::sync_with_stdio(false);
#define MAX_I INT_MAX //1e9
#define MIN_I INT_MIN //-1e9
#define MAX_UI UINT_MAX //1e9
#define MAX_LL LLONG_MAX //1e18
#define MIN_LL LLONG_MIN //-1e18
#define MAX_ULL ULLONG_MAX //1e19
  typedef long long ll;
  typedef pair<int,int> PII;
  typedef pair<char,char> PCC;
  typedef pair<ll,ll> PLL;
  typedef pair<char,int> PCI;
  typedef pair<int,char> PIC;
  typedef pair<ll,int> PLI;
  typedef pair<int,ll> PIL; 
  typedef pair<ll,char> PLC; 
  typedef pair<char,ll> PCL; 

inline void YesNo(bool b){ cout << (b?"Yes" : "No") << endl;}
inline void YESNO(bool b){ cout << (b?"YES" : "NO") << endl;}
inline void Yay(bool b){ cout << (b?"Yay!" : ":(") << endl;}
inline void out(bool b){ cout << (b?"Fennec" : "Snuke") << endl;}

const int V_MAX = 1e5+10;
int V;
vector<int> G[V_MAX];
bool ischecked[V_MAX];

int size(int p, int x){
  int s = 1;
  for(int &e : G[x]){
    if(e==p)continue;
    s += size(x,e);
  }
  return s;
}

int dfs(int x, int p, int y, vector<int> &v){
  if(x == y){
    v.push_back(x);
    return 1;
  }
  for(int &e:G[x]){
    if( e == p ) continue;
    int num = dfs(e,x,y,v);
    if(num!=0){
      v.push_back(x);
      return num+1;
    }
  }
  return 0;
}

int main(void){
  SPEEDUP
  cout << setprecision(15);
  cin >> V;
  rep(i,V-1){
    int x,y;cin >> x >> y;
    --x;--y;
    G[x].push_back(y);
    G[y].push_back(x);
  }
  vector<int> route;
  int num = dfs(0,-1,V-1,route);
  reverse(all(route));

  int count[2] = {0,0};
  int now[2] = {0,num-1};
  int turn = 0;
  while(true){
    int cur = route[now[turn]];
    if(ischecked[cur]) break;
    ischecked[cur] = true;
    ++count[turn];

    for(auto &e : G[cur]){
      if(now[turn] != num-1 && e == route[now[turn]+1]) continue;
      if(now[turn] != 0 && e == route[now[turn]-1])continue;
      count[turn] += size(cur,e);
    }

    if(turn) --now[turn];
    else ++now[turn];
    turn^=1;
  }

  out(count[0] > count[1]);
  return 0;
}


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

int main(){
    int n,k,i;
    cin>>n>>k;
    vector<int> data(n);
    rep(i,n){
        cin>>data[i];
    }
    sort(data.begin(),data.end());
    
    int ans=0;
    rep(i,k){
        ans+=data[i];
    }
    cout<<ans<<endl;
}


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

int main() {
  int N;
  cin >> N;
  vector<long long> A(3*N);
  for (int i = 0; i < 3*N; i++) cin >> A.at(i);
  sort(A.begin(), A.end());
  reverse(A.begin(), A.end());
  long long ans = 0;
  for (int i = 0; i < N; i++) {
    ans += A.at(2*i + 1);
  }
  cout << ans << endl;
}

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

/* 繰り返し自乗法. */
int modPow(long long a, long long n, long long p) {
	if (n == 0) return 1; // 0乗にも対応する場合.
	if (n == 1) return a % p;
	if (n % 2 == 1) return (a * modPow(a, n - 1, p)) % p;
	long long t = modPow(a, n / 2, p);
	return (t * t) % p;
}

signed main() {
    int N,K, R, S, P;
    cin >> N >> K;
    cin >> R >> S >> P;

    string T;
    cin >> T;
    rep (i, N-K) {
        if (T[i] == T[i+K]) T[i+K] = 'x';
    }
    ll sum = 0;
    rep (i, N) {
        if (T[i] == 's') sum += R;
        if (T[i] == 'r') sum += P;
        if (T[i] == 'p') sum += S;
    }

    cout << sum << endl;

}



Measured memory_bytes: 3740
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using static System.Console;
using static System.Math;
using static MyIO;
using static MyUtil;

public class C
{
	public static void Main() => (new Solver()).Solve();
}

public class Solver
{
	public void Solve()
	{
		int N = GetInt();
		int M = GetInt();
		int Q = GetInt();
		var a = new int[Q];
		var b = new int[Q];
		var c = new int[Q];
		var d = new int[Q];
		for(int i = 0; i < Q; i++)
		{
			a[i] = GetInt() - 1;
			b[i] = GetInt() - 1;
			c[i] = GetInt();
			d[i] = GetInt();
		}

		int ans = 0;
		for(int i = 1; i <= M; i++)
		{
			for(int j = i; j <= M; j++)
			{
				for(int k = j; k <= M; k++)
				{
					for(int l = k; l <= M; l++)
					{
						for(int m = l; m <= M; m++)
						{
							for(int n = m; n <= M; n++)
							{
								for(int o = n; o <= M; o++)
								{
									for(int p = o; p <= M; p++)
									{
										for(int q = p; q <= M; q++)
										{
											for(int r = q; r <= M; r++)
											{
												var A = new int[] { i, j, k, l, m, n, o, p, q, r };
												int sum = 0;
												for(int s = 0; s < Q; s++)
												{
													if(A[b[s]] - A[a[s]] == c[s])
														sum += d[s];
												}
												chmax(ref ans, sum);
											}
										}
									}
								}
							}
						}
					}
				}
			}
		}
		WriteLine(ans);
	}
}

public static class MyIO
{
	private static string[] args = null;
	private static int num = -1;
	private static int used = -1;

	private static string getArg()
	{
		if(used == num)
		{
			args = Console.ReadLine().Split(' ');
			num = args.Length;
			used = 0;
		}
		return args[used++];
	}

	public static int GetInt() => int.Parse(getArg());
	public static long GetLong() => long.Parse(getArg());
	public static double GetDouble() => double.Parse(getArg());
	public static decimal GetDecimal() => decimal.Parse(getArg());
	public static string GetString() => getArg();
	public static char GetChar() => getArg()[0];
	public static int[] GetInts(long N) => Enumerable.Range(0, (int)N).Select(_ => GetInt()).ToArray();
	public static long[] GetLongs(long N) => Enumerable.Range(0, (int)N).Select(_ => GetLong()).ToArray();
	public static double[] GetDoubles(long N) => Enumerable.Range(0, (int)N).Select(_ => GetDouble()).ToArray();
	public static decimal[] GetDecimals(long N) => Enumerable.Range(0, (int)N).Select(_ => GetDecimal()).ToArray();
	public static string[] GetStrings(long N) => Enumerable.Range(0, (int)N).Select(_ => GetString()).ToArray();
}

public static class MyUtil
{
	public static void chmax<T>(ref T x, T y) where T : IComparable<T>
	{
		if(x.CompareTo(y) < 0)
			x = y;
	}

	public static void chmin<T>(ref T x, T y) where T : IComparable<T>
	{
		if(x.CompareTo(y) > 0)
			x = y;
	}

	public static void swap<T>(ref T x, ref T y)
	{
		T tmp = x;
		x = y;
		y = tmp;
	}
}

Measured memory_bytes: 33288
#include<iostream>
#include<stack>

using namespace std;
typedef long long ll;
  
ll calc(ll x, string op, ll y) {
  if(op == "+") return x+y;
  if(op == "-") return x-y;
  if(op == "*") return x*y;
}

int main() {
  string str;
  ll numA,numB;
  stack<int> S;

  while(cin >> str) {
    if(str == "+" || str == "-" || str == "*") {
      numA = S.top(); S.pop();
      numB = S.top(); S.pop();
      S.push(calc(numB, str, numA));
    }else{
      numA = 0;
      for(int i = 0; i < (int)str.size(); i++){
        numA *= 10;
        numA += str[i] - '0';
      }
      S.push(numA);
    }
  }
  cout << S.top() << endl;

  return 0;
}


Measured memory_bytes: 3208
import java.util.Scanner;
public class Main{
 public static void main(String[] args){
   Scanner sc = new Scanner(System.in);
   int t_y = sc.nextInt();
   
   
   if(t_y==5 ||t_y==7 ||t_y==3){
    System.out.println("YES");
   }
   else
   {
    System.out.println("NO");
 
   }
   
 }
}

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

typedef pair<int, int> pii;

const int mod = 1000*1000*1000+7;

int po(int x, int n) {
    int ret = 1;
    while(n) {
        if(n & 1) ret = 1LL * ret * x % mod;
        x = 1LL * x * x % mod;
        n >>= 1;
    }
    return ret;
}
int inv(int x) { return po(x, mod - 2); }
int fact[5010], invf[5010];
int comb(int n, int k) {
    return 1LL * fact[n] * invf[k] % mod * invf[n - k] % mod;
}

int N;
vector<vector<int> > adj;
vector<int> sz;

void dfs(int u, int p) {
    sz[u] = 1;
    for(int i = 0; i < adj[u].size(); i++) {
        int v = adj[u][i];
        if(v == p) continue;
        dfs(v, u);
        sz[u] += sz[v];
    }
}

int dfs2(int u, int p) {
    for(int i = 0; i < adj[u].size(); i++) {
        int v = adj[u][i];
        if(v == p) continue;
        if(sz[v] > sz[0] / 2) return dfs2(v, u);
    }
    return u;
}

vector<int> tmp;
int ans = 0;

vector<vector<int> > cc;
int dp(int g, int k) {
    if(g == tmp.size()) return k == 0;
    int &ret = cc[g][k];
    if(ret != -1) return ret;

    ret = 0;
    for(int i = 0; i <= min(k, tmp[g]); i++) {
        ret += 1LL * comb(tmp[g], i) * comb(tmp[g], i) % mod * fact[i] % mod * dp(g + 1, k - i) % mod;
        ret %= mod;
    }
    return ret;
}

void solve() {
    int sum = 0;
    for(int i = 0; i < tmp.size(); i++) {
        sum += tmp[i];
    }

    cc = vector<vector<int> >(tmp.size(), vector<int>(sum + 1, -1));

    for(int i = 0; i <= sum; i++) {
        ans += 1LL * dp(0, i) * fact[sum - i] % mod * (i % 2? -1 : 1);
        ans = (ans % mod + mod) % mod;
    }
}

int main() {
    fact[0] = 1;
    for(int i = 1; i < 5010; i++) {
        fact[i] = 1LL * fact[i - 1] * i % mod;
    }
    for(int i = 0; i < 5010; i++) {
        invf[i] = inv(fact[i]);
    }

    scanf("%d", &N);

    adj.resize(N);
    for(int i = 0; i < N - 1; i++) {
        int u, v; scanf("%d %d", &u, &v);
        u--; v--;

        adj[u].push_back(v);
        adj[v].push_back(u);
    }

    sz.resize(N);
    dfs(0, -1);

    int C = dfs2(0, -1);

    for(int i = 0; i < adj[C].size(); i++) {
        int v = adj[C][i];
        if(N % 2 == 0 && sz[v] == N / 2) {
            printf("%d", 1LL * fact[N / 2] * fact[N / 2] % mod);
            return 0;
        }
    }

    dfs(C, -1);

    for(int i = 0; i < adj[C].size(); i++) {
        int v = adj[C][i];

        tmp.push_back(sz[v]);
    }
    solve();

    tmp.push_back(1);
    solve();

    printf("%d", ans);
}


Measured memory_bytes: 196736
#include <iostream>
#include <cstdlib>
using namespace std;
#define INF 1e9
#define MAX_E 500000
#define MAX_V 100000

struct edge {int u, v, cost;};
edge es[MAX_E];

int dis[MAX_V];
int V, E, r;

void bellman_ford(int s){
  for(int i = 0; i < V; i++) dis[i] = INF;
  dis[s] = 0;
  int count = 0;
  while(true){
    if(++count == V && count > 1){
      cout << "NEGATIVE CYCLE" << endl;
      exit(0);
    }
    bool update = false;
    for(int i = 0; i < E; i++){
      edge e = es[i];
      if(dis[e.u] != INF && dis[e.v] > dis[e.u] + e.cost){
        dis[e.v] = dis[e.u] + e.cost;
        update = true;
      }
    }
    if(!update) break;
  }
}

int main(void){
  int s, t, d;
  cin >> V >> E >> r;
  for(int i = 0; i < E; i++){
    cin >> s >> t >> d;
    edge e;
    e.u = s; e.v = t; e.cost = d;
    es[i] = e;
  }
  bellman_ford(r);
  for(int i = 0; i < V; i++){
    if(dis[i] == INF) cout << "INF" << endl;
    else cout << dis[i] << endl;
  }
  return 0;
}

Measured memory_bytes: 1192
#include <stdio.h>

int main() {
	long A, B, K;
	scanf("%ld %ld %ld", &A, &B, &K);
	
	if (K <= A) {
		printf("%ld %ld", A-K, B);
	}
	else if (K <= A + B) {
		printf("0 %ld", B+A-K);
	}
	else {
		printf("0 0");
	}

	return 0;
}

Measured memory_bytes: 128
#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<string>
#include<stack>
#include<queue>
#include<vector>
#include<algorithm>
#include <iomanip>
typedef long long int ll;
using namespace std;
#define FOR(i,a,b) for (int i=(a);i<(b);i++)
#define REP(i,n) for (int i=0;i<(n);i++)
#define EREP(i,n) for (int i=1;i<=(n);i++)
#define ALL(a) (a).begin(),(a).end()
#define EVEL 1

#ifndef EVEL
#define DEB(X) cout << #X << ":" <<X<<" " ;
#define TF(f) f ? cout<<"true  " : cout<<"false ";
#define END cout<<"\n";
#else
#define DEB(X) {X=X;}
#define TF(f) {f=f;}
#define END {}
#endif
const int MOD = 1000000007;
const int INF = 1000000;
#define NMAX 50
#define MAX 10

    int N,M,A,B;
    ll temp=0,id=0,mem,yui=1,sum=0;
    ll x[100010],y[100010];
    ll w,h;
    bool fir=true;



int main(){
    ios_base::sync_with_stdio(false);
    cin>>A;
    if(A<1200)cout<<"ABC";
    else cout<<"ARC";

    cout<<endl;
}
//ABC057 Maximum Average Sets


Measured memory_bytes: 256
n = int(input())
a = sorted(list(map(int,input().split())))
m = int(input())
b = sorted(list(map(int,input().split())))
i = 0
for t in b:
  f = 0
  while i<n:
    if a[i] == t:
      f = 1
      i += 1
      break
    i += 1
  if f == 0:
    print("NO")
    quit()
print("YES")


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

public class Main {

	public static void main(String[] args) {
		// TODO 自動生成されたメソッド・スタブ
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		
		int n = 0;
		int[] score = new int[2];
		
		try {
			n = Integer.parseInt(br.readLine());
			for (int i = 0; i < n; i++) {
				String[] cardStr = br.readLine().split("\\s");
				if (cardStr[0].compareToIgnoreCase(cardStr[1]) > 0) {
					score[0] = score[0] +3;
				} else if (cardStr[0].compareToIgnoreCase(cardStr[1]) < 0) {
					score[1] = score[1] +3;
				} else {
					score[0] = score[0] +1;
					score[1] = score[1] +1;
				}
			}
			System.out.println(score[0] + " " + score[1]);
			
		} catch (NumberFormatException e) {
			// TODO 自動生成された catch ブロック
			e.printStackTrace();
		} catch (IOException e) {
			// TODO 自動生成された catch ブロック
			e.printStackTrace();
		}
		
	}

}



Measured memory_bytes: 27196
#include <bits/stdc++.h>
#define ll long long int 
#define endl '\n'
#define INF 1000000000
#define MOD 1000000007
#define MAX 100000000 
#define mp make_pair
#define pb push_back
using namespace std;
map<int,int> m;
int main()
{
	ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int n,k;
    cin>>n>>k;
    int h[n],dp[n];
    for(int i=0;i<n;i++)
    {
    	cin>>h[i];
    	dp[i]=INT_MAX;
    }
    dp[0]=0;
    for(int i=0;i<n;i++)
    {
    	int j=1;
    	while(j<=k&&(i+j)<n)
    	{
    		dp[i+j]=min(dp[i+j],dp[i]+abs(h[i+j]-h[i]));
    		j++;
    	}
    }
    cout<<dp[n-1]<<endl;
}

Measured memory_bytes: 4244
#include <iostream>
#include <string>
#include <algorithm>
#include <functional>
#include <vector>
#include <utility>
#include <cstring>
#include <iomanip>
#include <numeric>
#include <limits>
#include <cmath>
#include <cassert>
#include <map>

using namespace std;
using ll = long long;

const int INF = 1<<30;
const int MOD = (int)1e9 + 7;
const int MAX_N = (int)1e5 + 5;
#define debug(x) cout << #x << ": " << x << endl

signed main(void)
{
    cin.tie(0);
    ios::sync_with_stdio(false);
    string s; cin >> s;
    map<char, int> mp;
    for(int i = 0; i < s.size(); i++)
    {
        mp[s[i]]++;
    }
    bool ok = true;
    for(auto x : mp)
    {
        if(x.second != 2) ok = false;
    }
    cout << (ok ? "Yes" : "No") << endl;
    return 0;
}



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

/*int n;
  cin >> n;
  vector<int> h(n);*/

int main() {

  int ans=0;
  int ans1=0;
  int n;
  cin >> n;
  vector<int> h(n);



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

  for(int i=0;i<n;i++){
    for(int j=0;j<n;j++){
      ans += h[i] * h[j];
    }
  }

  for(int i=0;i<n;i++){
    ans1 += h[i] * h[i]; 
  }

  ans -= ans1;
  ans /= 2;

  cout << ans << endl;

  return 0;
}

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

using namespace std;

typedef int _loop_int;
#define REP(i,n) for(_loop_int i=0;i<(_loop_int)(n);++i)
#define FOR(i,a,b) for(_loop_int i=(_loop_int)(a);i<(_loop_int)(b);++i)
#define FORR(i,a,b) for(_loop_int i=(_loop_int)(b)-1;i>=(_loop_int)(a);--i)

#define DEBUG(x) cout<<#x<<": "<<x<<endl
#define DEBUG_VEC(v) cout<<#v<<":";REP(i,v.size())cout<<" "<<v[i];cout<<endl
#define ALL(a) (a).begin(),(a).end()

#define CHMIN(a,b) a=min((a),(b))
#define CHMAX(a,b) a=max((a),(b))

int n,m;
int g[25][21];

int subedge[1<<15][15];

int dp[1<<15][15];

int main(){
  scanf("%d%d",&n,&m);
  REP(i,n)REP(j,n)g[i][j]=-1025830252;
  REP(i,m){
    int a,b,c;
    scanf("%d%d%d",&a,&b,&c);
    --a;--b;
    g[a][b] = g[b][a] = c;
  }
  // calc subset edge sum
  {
    int po = (1<<n)-2;
    for(int i=po;i>0;i=(i-1)&po){
      REP(j,n)if((i>>j)&1)REP(k,j)if((i>>k)&1){
        subedge[i][0] += max(0,g[j][k]);
      }
      FOR(j,1,n)subedge[i][j] = subedge[i][0];
      REP(j,n){
        REP(k,n)if((i>>k)&1){
          subedge[i][j] += max(0,g[j][k]);
        }
      }
    }
  }
  // calc answer
  int ans = 0;
  REP(i,1<<n)REP(j,n)dp[i][j] = -1025830252;
  dp[1][0] = 0;
  REP(mask,1<<n)if(mask&1){
    int neg = (~mask) & ((1<<n)-1);
    REP(i,n)if(dp[mask][i]>=0){
      if(i==n-1){
        CHMAX(ans,dp[mask][i]);
      }
      // next terminal
      REP(to,n)if(((mask>>to)&1)==0){
        CHMAX(dp[mask|(1<<to)][to], dp[mask][i]+g[i][to]);
      }
      // terminal add
      for(int s=neg;s>0;s=(s-1)&neg){
        CHMAX(dp[mask|s][i], dp[mask][i]+subedge[s][i]);
      }
    }
  }

  int total = 0;
  REP(i,n)REP(j,i)total+=max(0,g[i][j]);
  printf("%d\n",total-ans);
  return 0;
}


Measured memory_bytes: 4096
n = int(input())
f = [list(map(int,input().split())) for i in range(n)]
p = [list(map(int,input().split())) for i in range(n)]

s = [list(map(int,bin(x)[2:].zfill(10))) for x in range(1,2**10)]
c = 0
ans = 0
max = -1000000000
for k in range(2**10-1):
    for i in range(n):
        for j in range(10):
            c += f[i][j]*s[k][j]
        ans += p[i][c]
        c=0
    if ans > max:
        max = ans
    ans = 0
print(max)


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

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    int x,n;
    cin>>x>>n;
    vector<int> vec(n);
    for(int i=0;i<n;++i)
    {
        cin>>vec[i];
    }
    sort(vec.begin(),vec.end());
    int ans;
    for(int i=0;i<=101;++i)
    {
        int temp1=x-i;
        int temp2=x+i;
        if(!binary_search(vec.begin(),vec.end(),temp1))
        {
            ans=temp1;
            break;
        }
        else if(!binary_search(vec.begin(),vec.end(),temp2))
        {
            ans=temp2;
            break;
        }
        
    }
    
    cout<<ans<<"\n";

}


Measured memory_bytes: 3644
n = int(input())
op = []
for _ in range(n):
    l = list(map(int, input().split()))
    op.append(l)
score = []
for _ in range(n):
    l = list(map(int, input().split()))
    score.append(l)
final = -999999999999999999999999


for pat in range(1,2**10):
    openNum = [0] * n
    for i in range(10):
        if ((pat >> i) & 1) == 1:
            for j in range(n):
                if op[j][i] == 1:
                    openNum[j] += 1
    res = 0
    for i in range(n):
        res += score[i][openNum[i]]
    final = max(final, res)
print(final)


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

using namespace std;

int X[6];

int main()
{
    for(int i = 1; i<=5; i++){
        cin>>X[i];
    }
    
    for(int i =1; i<=5; i++){
        if(X[i] == 0){
            cout<<i<<endl;
            break;
        }
    }
    return 0;
}


Measured memory_bytes: 3580
#include <bits/stdc++.h>
#define REP(i,  n) for (int i  =  0; i < (int)(n); ++i)
#define REPR(i,  n) for (int i  =  n - 1; i >= 0; --i)
using namespace std;
#define int long long int
//------------ATCODER TEMPLATE-------------

signed main() {
  int N; cin >> N;
  int D; cin >> D;
  D = D * D;

  int cnt = 0;
  for (int i = 0; i < N; ++i) {
    int X, Y; cin >> X >> Y;
    int d = X * X + Y * Y;
    if (d <= D) ++cnt;
  }
  cout << cnt << endl;
  return 0;
}


Measured memory_bytes: 3632
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<iostream>
#include<string>
#include<vector>
#include<map>
#include<set>
#include<stack>
#include<list>
#include<queue>
#include<deque>
#include<algorithm>
#include<numeric>
#include<utility>
#include<complex>
#include<functional>
using namespace std;
const int MAX_N=100;
const int MAX_M=100;
int N;
int M;
int C;
int A[MAX_N][MAX_M];
int B[MAX_M];
void solve(){
    int res=0;
    int sum=0;
    for(int i=0;i<N;i++){
        for(int j=0;j<M;j++){
            sum+=B[j]*A[i][j];
        }
        if(sum>-C) res++;
        sum=0;
}
cout<<res<<'\n';
}
int main()
{
    cin.tie(0);
    ios::sync_with_stdio(false);
    cin >> N;
    cin >> M;
    cin >> C;
    for(int i=0;i<M;i++) cin>>B[i];
    for(int i=0;i<N;i++){
        for(int j=0;j<M;j++){
            cin>>A[i][j];
        }
    }
    solve();
}

Measured memory_bytes: 256
#include <iostream>
#include <string>
#include <sstream>
#include <iomanip>
#include <vector>
#include <map>
#include <set>
#include <algorithm>
#include <numeric>
#include <queue>

using namespace std;

void Diverta2019_C()
{
	int N;
	cin >> N;
	vector<string> str(N);
	for (auto& x : str)
		cin >> x;

	int ct = 0;
	int f = 0; // begin with 'B'
	int b = 0; // end with 'A'
	int d = 0; // both
	for (int i = 0; i < N; ++i)
	{
		const auto& x = str[i];
		bool isF = x.front() == 'B';
		bool isB = x.back() == 'A';
		if (isF && isB)
			++d;
		else if (isF)
			++f;
		else if (isB)
			++b;

		bool isA = false;
		for (auto y : x)
		{
			if (isA && y == 'B')
				++ct;
			isA = y == 'A';
		}
	}
	ct += max(0, d - 1);
	if (f > 0 && d > 0)
	{
		--f;
		++ct;
	}
	if (b > 0 && d > 0)
	{
		--b;
		++ct;
	}
	ct += min(f, b);

	cout << ct << endl;
}
int main()
{
	Diverta2019_C();
	return 0;
}

Measured memory_bytes: 768