akhauriyash/Code-Regression huggingface dataset:
package main

import (
	"bufio"
	"fmt"
	"os"
	"sort"
	"strconv"
)

func out(x ...interface{}) {
	fmt.Println(x...)
}

var sc = bufio.NewScanner(os.Stdin)

func getInt() int {
	sc.Scan()
	k, e := strconv.Atoi(sc.Text())
	if e != nil {
		panic(e)
	}
	return k
}

func getString() string {
	sc.Scan()
	return sc.Text()
}

// min, max, asub, absなど基本関数
func max(a, b int) int {
	if a > b {
		return a
	}
	return b
}

func min(a, b int) int {
	if a < b {
		return a
	}
	return b
}

func asub(a, b int) int {
	if a > b {
		return a - b
	}
	return b - a
}

func abs(a int) int {
	if a >= 0 {
		return a
	}
	return -a
}

func lowerBound(a []int, x int) int {
	idx := sort.Search(len(a), func(k int) bool {
		return a[k] >= x
	})
	return idx
}

func upperBound(a []int, x int) int {
	idx := sort.Search(len(a), func(k int) bool {
		return a[k] > x
	})
	return idx
}

const mod = 998244353

func mpow(n, p int) int {
	if p == 0 {
		return 1
	}
	ret := 1
	x := n
	for p > 0 {
		if p%2 == 1 {
			ret *= x
			ret %= mod
		}
		x *= x
		x %= mod
		p /= 2
	}
	return ret
}

// func mpow(a, b int) int {
// 	if b == 0 {
// 		return 1
// 	} else if b%2 == 0 {
// 		tmp := mpow(a, b/2)
// 		return tmp * tmp % mod
// 	}
// 	return mpow(a, b-1) * a % mod
// }

var fact [maxval]int
var ifact [maxval]int

func nCk(n, k int) int {
	if n < k || k < 0 {
		return 0
	}
	ret := (fact[n] * ifact[k]) % mod
	ret *= ifact[n-k]
	ret %= mod
	return ret
}

const maxval = 220000

func main() {
	sc.Split(bufio.ScanWords)

	fact[0] = 1
	for k := 1; k < maxval; k++ {
		fact[k] = (k * fact[k-1]) % mod
	}
	ifact[maxval-1] = mpow(fact[maxval-1], mod-2)
	for k := maxval - 2; k >= 0; k-- {
		ifact[k] = ((k + 1) * ifact[k+1]) % mod
	}

	N, M, K := getInt(), getInt(), getInt()

	ans := 0
	for k := 0; k <= K; k++ {
		if N < k {
			break
		}
		x := M * mpow(M-1, N-1-k)
		x %= mod
		x *= nCk(N-1, k)
		x %= mod
		ans += x
		ans %= mod
	}

	out(ans)
}


Measured memory_bytes: 5212
n, k, c = map(int, input().split())
s = input()

def getPositions():
    res = []
    i = 0
    while i < n:
        if s[i] == 'o':
            res.append(i)
            i += c + 1
        else:
            i += 1
    return res

l = getPositions()

s = s[::-1]
r = getPositions()
for i in range(k):
    r[i] = n-1-r[i]
s = s[::-1]

lastL = [-1]*(n+1)
for i in range(k):
    lastL[l[i]+1] = i

for i in range(n):
    if lastL[i+1] == -1:
        lastL[i+1] = lastL[i]

lastR = [-1] * (n+1)
for i in range(k):
    lastR[r[i]] = i
for i in range(n-1, -1, -1):
    if lastR[i] == -1:
        lastR[i] = lastR[i+1]

for i in range(n):
    if s[i] == 'x': continue
    li = lastL[i]
    ri = lastR[i+1]

    cnt = 0
    if li != -1: cnt += li+1
    if ri != -1: cnt += ri+1
    if (li != -1) and (ri != -1) and (r[ri]-l[li] <= c): cnt -= 1
    if cnt >= k: continue
    print(i+1)

Measured memory_bytes: 36568
# include <iostream>
# include <algorithm>
# include <vector>
# include <string>
# include <set>
# include <map>
# include <cmath>
# include <iomanip>
# include <functional>
# include <utility>
# include <stack>
# include <queue>
# include <list>
# include <tuple>
# include <unordered_map>
# include <numeric>
# include <complex>
# include <bitset>
# include <random>
# include <chrono>
# include <cstdlib>
# include <tuple>
# include <array>
using namespace std;
using LL = long long;
using ULL = unsigned long long;
constexpr int INF = 2147483647;
constexpr int HINF = INF / 2;
constexpr double DINF = 100000000000000000.0;
constexpr double HDINF = 50000000000000000.0;
constexpr long long LINF = 9223372036854775807;
constexpr long long HLINF = 4500000000000000000;
const double PI = acos(-1);
template <typename T_char>T_char TL(T_char cX) { return tolower(cX); };
template <typename T_char>T_char TU(T_char cX) { return toupper(cX); };
const int vy[] = { -1, -1, -1, 0, 1, 1, 1, 0 }, vx[] = { -1, 0, 1, 1, 1, 0, -1, -1 };
const int dx[4] = { 0,1,0,-1 }, dy[4] = { 1,0,-1,0 };
int popcnt(unsigned long long n) { int cnt = 0; for (int i = 0; i < 64; i++)if ((n >> i) & 1)cnt++; return cnt; }
# define ALL(qpqpq)           (qpqpq).begin(),(qpqpq).end()
# define RALL(qpqpq)           (qpqpq).rbegin(),(qpqpq).rend()
# define UNIQUE(wpwpw)        sort(ALL((wpwpw)));(wpwpw).erase(unique(ALL((wpwpw))),(wpwpw).end())
# define LOWER(epepe)         transform(ALL((epepe)),(epepe).begin(),TL<char>)
# define UPPER(rprpr)         transform(ALL((rprpr)),(rprpr).begin(),TU<char>)
# define FOR(i,tptpt,ypypy)   for(LL i=(tptpt);i<(ypypy);i++)
# define RFOR(i,tptpt,ypypy)  for(LL i=(tptpt);i>=(ypypy);i--)
# define REP(i,upupu)         FOR(i,0,upupu)
# define INIT                 std::ios::sync_with_stdio(false);std::cin.tie(0)

int n;
int d1, d2;
vector<pair<int,int>> ng[2];
int col[1010][1010];
bool used[1010][1010];

void dfs(int y, int x, int t, int c) {
	used[y][x] = true;
	if (c == 1)col[y][x] += 1 << t;
	for (int i = 0; i<ng[t].size(); i++) {
		int nx = ng[t][i].first;
		int ny = ng[t][i].second;
		for (int j = 0; j<4; j++) {
			if (nx + x >= 0 && nx + x<2 * n && ny + y >= 0 && ny + y<2 * n) {
				if (!used[ny + y][nx + x]) {
					dfs(ny + y, nx + x, t, 1 - c);
				}
			}
			if (j == 0)nx = -nx;
			if (j == 1)ny = -ny;
			if (j == 2)nx = -nx;
		}
	}
}

int main() {
	cin >> n >> d1 >> d2;
	REP(i, 2 * n) {
		REP(j, 2 * n) {
			int x = i*i + j*j;
			if (x == d1) {
				ng[0].emplace_back(make_pair(i, j));
			}
			if (x == d2) {
				ng[1].emplace_back(make_pair(i, j));
			}
		}
	}
	REP(k, 2) {
		REP(i, 1010)REP(j, 1010)used[i][j] = false;
		REP(i, 2 * n)REP(j, 2 * n) {
			if (!used[i][j]) {
				dfs(i, j, k, 0);
			}
		}
	}
	REP(i, 4) {
		int cnt = 0;
		REP(j, 2 * n)REP(k, 2 * n)if (i == col[j][k])cnt++;
		if (cnt >= n*n) {
			int cntt = 0;
			REP(j, 2 * n)REP(k, 2 * n) {
				if (cntt == n*n)return 0;
				if (i == col[j][k]) {
					cout << j << " " << k << endl;
					cntt++;
				}
			}
			return 0;
		}
	}
}

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

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cout<<setprecision(std::numeric_limits<float>::max_digits10);
  cout<<fixed;
  int a,b;
  cin>>a>>b;
  if(a<=5)
    cout<<0<<endl;
  else if(a<=12)
    cout << b/2 << endl;
  else
    cout << b << endl;
  return 0;
}


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

#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 ALL(obj) (obj).begin(), (obj).end()
#define INF 1e9
typedef long long ll;


int main() {
    int N,K;
    cin >> N >> K;
    deque<int> V_origin;
    REP(i,N){
        int v;
        cin >> v;
        V_origin.push_back(v);
    }


    int ans = 0;
    int KK = K;
    K = min(K,N);

    REP(i,K/2+1){
        REP(j,K-i+1){
            //cout << "front: "<< j <<  ", back: " << K-i-j << ", return: " << i << endl;
            deque<int> V(V_origin);
            vector<int> my;
            REP(k,j){
                int d = V.front();
                V.pop_front();
                my.push_back(d);
            }
            FOR(k,j+1,K-i+1){
                int d = V.back();
                V.pop_back();
                my.push_back(d);
            }
            sort(ALL(my));

            REP(x,KK-K+i+1){
                int count = 0;
                FOR(k,x,my.size()){
                    count += my[k];
                }
                ans = max(ans,count);
                //cout << count << endl;
            }
        }
    }
    ans = max(ans, 0);
    cout << ans << endl;
    return 0;
}

Measured memory_bytes: 3628
require 'pp'
$DEBUG ? define_method(:dpp){|e|pp e} : define_method(:dpp){|e|}


# 空白区切の入力値を数値の配列で返却する
def gets_i_list()
  gets.chomp.split(" ").map(&:to_i)
end
x_list = gets_i_list

x_list.each_with_index do |x, i|
  if x == 0
    puts i+1
    exit
  end
end

Measured memory_bytes: 14504
#include <iostream>
#include <sstream>
#include <algorithm>
#include <string>
#include <vector>
#include <stack>
#include <set>
#include <queue>
#include <map>
#include <numeric>
#include <functional>
#include <math.h>

using namespace std;

typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;

#define INF ((int)1e9)
#define INFLL ((ll)1e18)
#define MOD (1000000007LL)


int main(int argc, const char * argv[]) {
    ios_base::sync_with_stdio(false);
    ll N;
    cin >> N;
    
    if (N == 1) {
        cout << 0 << endl;
        return 0;
    }
    
    map<ll, int> Map;
    for (ll n = 2; n * n <= N; ++n) {
        while (N%n == 0) {
            Map[n]++;
            N /= n;
        }
    }
    if (N != 1) {
        Map[N] = 1;
    }
    
    vector<int> Sum(11);
    for (int i = 1; i <= 10; ++i) {
        Sum[i] = Sum[i-1] + i;
    }
    int Ans = 0;
    for (auto& z : Map) {
        int k = z.second;
        for (int i = 1; i <= 10; ++i) {
            if (k < Sum[i]) {
                Ans += i-1;
                break;
            }
        }
    }
    cout << Ans << endl;

    return 0;
}


Measured memory_bytes: 3676
#include <iostream>
#include <algorithm>
#include <vector>

#define MAXV 10000
using namespace std;

int main() {
    int n;
    cin >> n;
    vector<int> cnt(MAXV, 0);

    for (int i = 0; i < n; i++) {
        int temp;
        cin >> temp;
        cnt[temp]++;
    }
    int count = 0;
    for (int i = 0; i < cnt.size(); i++) {
        while (cnt[i]) {
            if (count)
                cout << " ";
            cout << i;
            count++;
            cnt[i]--;
        }
    }
    cout << endl;
    return 0;
}


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

int main(){
  int n,flag=0;
  cin >> n;
  int a[n];
  map<int,int> mp;
  for(int i=0;i<n;i++){
    cin >> a[i];
    mp[a[i]]++;
  }
  for(auto i:mp){
    if(i.second>1) flag = 1;
  }
  if(flag) cout << "NO" << endl;
  else cout << "YES" << endl;
}

Measured memory_bytes: 10368
#include <algorithm>
#include <functional>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <string>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <vector>
#include <list>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <bitset>
#include <climits>
#include <complex>


#define all(c) (c).begin(), (c).end()
#define rep(i,n) for(int i=0;i<(n);i++)
#define pb(e) push_back(e)
#define mp(a, b) make_pair(a, b)
#define fr first
#define sc second

const int INF=100000000;
int dx[4]={1,0,-1,0};
int dy[4]={0,1,0,-1};
using namespace std;
typedef pair<int ,int > P;
typedef long long ll;
typedef complex<double> Point;


struct Circle {
    Point c;
    double r;
    int color;
    Circle(Point c=Point(), double r=0.0,int color=0) :
        c(c),r(r),color(color){}
};
double getDistance(Point a,Point b) {
    return abs(a-b);
}

bool intersect(Circle c1, Circle c2) {
    double d=getDistance(c1.c,c2.c);
    return d<c1.r+c2.r;
}
int n;
bool same_color[30][30];
bool is_intersect[30][30];
bool ok(int n,int S) {
    rep(i,n) {
        if(!(S>>i&1)&&is_intersect[i][n]) return false;
    }

    return true;
}
short dp[1<<24];
int dfs(int S) {
    if(dp[S]!=-1) return dp[S];
    int ret=0;
    rep(i,n) rep(j,n) if(i!=j) {
        if(((S>>i)&1)) continue;
        if(((S>>j)&1)) continue;
        if(!same_color[i][j]) continue;
        if(!ok(i,S)) continue;
        if(!ok(j,S)) continue;

        //printf("remove %d %d\n",i,j);
        ret=max(ret,dfs(S|(1<<i)|(1<<j))+2);
    }

    return dp[S]=ret;
}
void solve() {
    memset(dp,-1,sizeof(dp));
    rep(i,30) rep(j,30) is_intersect[i][j]=false;
    rep(i,30) rep(j,30) same_color[i][j]=false;
    vector<Circle> circles;
    rep(i,n) {
        double x,y,r;
        int c;
        cin>>x>>y>>r>>c;
        circles.push_back(Circle(Point{x,y},r,c));
    }
    rep(i,circles.size()) rep(j,circles.size()) {
        if(intersect(circles[i],circles[j])) is_intersect[i][j]=true;
        if(circles[i].color==circles[j].color) same_color[i][j]=true;
    }
    cout<<dfs(0)<<endl;
}
int main() {
    while(cin>>n) {
        if(!n) break;
        solve();
    }
    return 0;
}

Measured memory_bytes: 34032
n=gets.to_i
p=gets.chomp.split(" ").map(&:to_i)
cnt=0
n.times do|i|
	if(p[i]!=(i+1))
		if(cnt==0)
			idx=p.index(i+1)
			w=p[i]
			p[i]=p[idx]
			p[idx]=w
			cnt+=1
		else
			cnt+=1
			break
		end
	end
end
if(cnt<=1)
	puts("YES")
else
	puts("NO")
end

Measured memory_bytes: 1788
#include <iostream>
#include <vector>
#include <algorithm>

#define FORR(i,b,e) for(int i=(b);i<(int)(e);++i)
#define FOR(i,e) FORR(i,0,e)
#define dump(var) cerr << #var ": " << var << "\n"
#define dumpc(con) for(auto& e: con) cerr << e << " "; cerr<<"\n"

typedef long long ll;
typedef unsigned long long ull;

const double EPS = 1e-6;
const int d4[] = {0, -1, 0, 1, 0};

using namespace std;

struct UnionFind {
    vector<int> v;
    int groups;
    UnionFind() {}
    UnionFind(int n): groups(n) {
        v.resize(n+1);
        FORR(i, 1, n+1) v[i] = i;
    }
    int find(int p) {
        if (v[p] == p) return p;
        return v[p] = find(v[p]);
    }
    int merge(int p, int q) {
        p = find(p);
        q = find(q);
        if (p != q) {
            v[q] = p;
            groups--;
        }
        return groups;
    }
};

struct Edge {
    int s, t, w;
    bool operator<(const Edge &o) const {
        return w < o.w;
    }
};

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

    int V, E;
    cin >> V >> E;
    vector<Edge> edge(E);
    FOR(i, E) {
        auto &e = edge[i];
        cin >> e.s >> e.t >> e.w;
    }
    sort(edge.begin(), edge.end());

    UnionFind uf(V);
    int weight = 0;
    for (auto &e: edge) {
        if (uf.find(e.s) != uf.find(e.t)) {
            weight += e.w;
            uf.merge(e.s, e.t);
            if (uf.groups == 1) break;
        }
    }
    
    cout << weight << endl;

    return 0;
}

Measured memory_bytes: 2424
#include<iostream>
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<queue>
#define maxn 1000005
using namespace std;
typedef long long ll;
const ll mod=1e9+7;
int n,m,s,t,tot;
ll val[maxn];
int head[maxn],nex[maxn],to[maxn],id[maxn];
void add(int x,int y,int z)
{
	to[++tot]=y; val[tot]=z; nex[tot]=head[x]; head[x]=tot;
}
queue<int> q;
ll dp1[maxn],dp2[maxn],dis1[maxn],dis2[maxn],vis[maxn],dis3[maxn];
int cmp(int i,int j)
{
	return dis3[i]<dis3[j];
}
void spfa(int start,ll *dp,ll *dis)
{
	for(int i=0;i<=n;i++)
		dis[i]=12345678912331231ll,id[i]=i;

	dis[start]=0; dp[start]=1;
	q.push(start);
	while(!q.empty())
	{
		int now=q.front(); q.pop(); vis[now]=0;
		for(int i=head[now];i;i=nex[i])
		{
			if(dis[to[i]]>dis[now]+val[i])
			{
				dis[to[i]]=dis[now]+val[i];
				if(!vis[to[i]])
				{
					vis[to[i]]=1;
					q.push(to[i]);
				}
			}
		}
	}

	memcpy(dis3,dis,sizeof(ll)*maxn);

	sort(id+1,id+1+n,cmp);

	for(int i=1;i<=n;i++)
	{
		int now=id[i];
//		cerr<<dis2[i]<<endl;
//		cerr<<id[i]<<endl;
		for(int j=head[now];j;j=nex[j])
		{
			if(dis[to[j]]==dis[now]+val[j])
			{
				dp[to[j]]+=dp[now];
				dp[to[j]]%=mod;
			}
		}
	}

}
int main()
{
	scanf("%d%d%d%d",&n,&m,&s,&t);
	for(int i=1;i<=m;i++)
	{
		int x,y,z;
		scanf("%d%d%d",&x,&y,&z);
		add(x,y,z);
		add(y,x,z);
	}
	spfa(s,dp1,dis1);
	spfa(t,dp2,dis2);
	ll d=dis1[t];
//	cerr<<d<<endl;
  	ll ans=(dp1[t]*dp1[t])%mod;
//	cerr<<"its "<<dp1[t]<<" "<<dp2[s]<<endl;
	for(int i=1;i<=n;i++)
	{
		if(dis1[i]*2==d && dis2[i]*2==d)
		{
			ans-=((dp1[i]*dp2[i])%mod*((dp1[i]*dp2[i])%mod)%mod);
//			cerr<<"its "<<i<<" "<<dp1[i]<<" "<<dp2[i]<<endl;
			ans=(ans%mod+mod)%mod;
		}
	}

	for(int i=1;i<=n;i++)
	{
		for(int j=head[i];j;j=nex[j])
		{
//			cerr<<i<<" "<<to[j]<<" "<<dis1[i]<<" "<<val[j]<<" "
			if(dis1[i]*2<d && dis2[to[j]]*2<d && dis1[i]+dis2[to[j]]+val[j]==d)
			{
//				cerr<<"+1"<<endl;
				ans-=(dp1[i]*dp2[to[j]])%mod*((dp1[i]*dp2[to[j]])%mod)%mod;
				ans=(ans%mod+mod)%mod;
			}
		}
	}

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


Measured memory_bytes: 41600
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define REP(i,m,n) for(int i=(int)(m); i<(int)(n); i++)
#define rep(i,n) REP(i,0,n)
const int inf = 1e9+7;
const ll longinf = 1LL<<60;
const ll mod = 1e9+7;

void dfs(vector<vector<int> >& B, int tmp, int len, vector<bool>& vis, vector<bool>& vis2, bool& flag, vector<int>& depth) {
  if(flag) return;
  if(B[tmp].empty()) {
    depth[tmp] = max(depth[tmp], 1);
  } else {
    for(auto ne: B[tmp]) {
      if(vis[ne]) {
        if(vis2[ne]) flag = true;
        depth[tmp] = max(depth[tmp], depth[ne]+1);
      } else {
        vis[ne] = true;
        vis2[ne] = true;
        dfs(B, ne, len+1, vis, vis2, flag, depth);
        depth[tmp] = max(depth[tmp], depth[ne]+1);
        vis2[ne] = false;
      }
    }
  }
}

int main() {
  int N;
  cin >> N;
  vector<vector<int> > mp(N, vector<int>(N));
  {
    int cnt = 0;
    rep(i, N) {
      REP(j, i+1, N) {
        mp[i][j] = cnt;
        mp[j][i] = cnt;
        cnt++;
      }
    }
  }
  vector<vector<int> > A(N, vector<int>(N-1));
  int M = N*(N-1)/2;
  vector<vector<int> > B(M), C(M);
  rep(i, N) {
    int prev;
    rep(j, N-1) {
      cin >> A[i][j];
      A[i][j]--;
      int tmp = mp[i][A[i][j]];
      if(j > 0) {
        B[prev].push_back(tmp);
        C[tmp].push_back(prev);
      }
      prev = tmp;
    }
  }
  bool flag = false, flag2 = false;
  vector<bool> vis(M);
  vector<int> depth(M);
  rep(i, C.size()) {
    if(C[i].empty()) {
      flag2 = true;
      vis[i] = true;
      vector<bool> vis2(M);
      vis2[i] = true;
      dfs(B, i, 1, vis, vis2, flag, depth);
    }
  }
  int ans = -1;
  rep(i, M)
    ans = max(ans, depth[i]);
  if(flag || !flag2)
    cout << -1 << endl;
  else
    cout << ans << endl;
  return 0;
}


Measured memory_bytes: 65152
#include<bits/stdc++.h>
using namespace std;
const int N=110;
int gi() {
    int x=0,o=1;char ch=getchar();
    while((ch<'0'||ch>'9')&&ch!='-') ch=getchar();
    if(ch=='-') o=-1,ch=getchar();
    while(ch>='0'&&ch<='9') x=x*10+ch-'0',ch=getchar();
    return x*o;
}
char s[N];
int n;
int work(int c) {
    int cnt=0,ret=0;
    for(int i=1;i<=n;i++) {
        if(s[i]-'a'!=c) ++cnt;
        else cnt=0;
        ret=max(ret,cnt);
    }
    return ret;
}
int main() {
    scanf("%s",s+1),n=strlen(s+1);
    int ans=1e9;
    for(int i=0;i<26;i++) ans=min(ans,work(i));
    printf("%d\n",ans);
    return 0;
}


Measured memory_bytes: 256
#include <bits/stdc++.h>
#include <set>
using namespace std;
int main(){
  //int型のsetの宣言
  set <int> S;
  
  int N;
  cin >> N;
  
  for(int i = 0;i < N;i++) {
    int d;
    cin >> d;
    S.insert(d);
  }
  cout << S.size() << endl;
}


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


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

  long long answer=0;
  
  for (int i=0;i<n;i++){
    long long tmp;
    cin>>tmp;
    answer+=tmp-1;
  }
  
  cout<<answer<<endl;
  
  


  
  
  

  
  
}

Measured memory_bytes: 256
#include<iostream>
#include<algorithm>

using namespace std;

const int maxn = 100000+5;
const int inf = 0x3f3f3f3f;
const int maxm=100000+5;

long long a[maxn],t[maxn],f[maxm];

int main(){
	int n,m;
	cin>>n>>m;
    for(int i=1;i<=n;i++){
    	cin>>t[i]>>a[i];
    }
    for(int j=1;j<=m;j++){
    	f[j]=-inf;
    }
    f[0]=0;
    for(int i=1;i<=n;i++){
    	for(int j=m;j>=0;j--){
    		if(j>=t[i]){
    			f[j]=max(f[j],f[j-t[i]]+a[i]);
    		}	
    		}
    }
    long long ret=-1;
    for(int j=0;j<=m;j++){
    	ret=max(ret,f[j]);
    }
cout<<ret <<endl;
return 0;
	
}

Measured memory_bytes: 1024
//競技プログラミング用のテンプレート
#include <iostream>
#include <vector>
#include <algorithm>
#include <map>
#include <queue>
#include <string>
#include <math.h>
#include <stack>
#include <limits>

#define rep(i,n) for(int i=0, i##_len=(n); i<i##_len; ++i)
#define repr(i, n) for(int i = n - 1; i >= 0; i--)

using namespace std;
//エイリアス
using ll = long long int;
using p = pair<int, int>;
using pl = pair<ll, ll>;
using v = vector<int>;
using vd = vector<double>;
using vs = vector<string>;
using vl = vector<ll>;

//定数
const int intmax = numeric_limits<int>::max();
const ll llmax = numeric_limits<ll>::max();
const ll mod = 1e9 + 7;
const double pi = M_PI;

string cand[] = {"AC", "WA", "TLE", "RE"};

int main()
{
  int n;
  cin >> n;
  map<string, int> memo;
  rep(i, n)
  {
    string s;
    cin >> s;
    memo[s]++;
  }
  rep(i, 4) {
    printf("%s x %d\n", cand[i].c_str(), memo[cand[i]]);
  }
}


Measured memory_bytes: 3720
N=int(input())
A=list(map(int,input().split()))
A.sort(reverse=True)
if N%2==0:
    x=A[0]
    x+=sum(A[1:N//2])*2
    print(x)
else:
    x=A[0]
    x+=sum(A[1:(N-1)//2])*2
    x+=A[(N-1)//2]
    print(x)


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

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

	while(t>0){
		s += t%10;
		t *= 0.1;
	}
	if(n%s==0)
		cout << "Yes" << endl;
	else
		cout << "No" << endl;

	return 0;
}

Measured memory_bytes: 3532
#include <cstdio>
#include <climits>
#include <iostream>
#include <string>
#include <vector>
#include <set>
#include <cmath>
#include <stack>
#include <algorithm>
#include <random>
#include <iomanip>
#include <map>
#include <queue>
#include <functional>
#include <numeric>
#include <chrono>
#include <cstdlib>
using ll = long long;
using namespace std;

const ll MOD = 1e9 + 7;
const double pi = acos(-1);
typedef pair<int, int> P;
#define REP(i, n) for (int(i) = 0; (i) < (n); ++(i))
#define repi(i, a, b) for (int i = int(a); i < int(b); ++i)
#define EPS 1e-4
#define OUTPUT(i) (cout << (ll)i << endl)
#define ALL(a) ((a).begin(), (a).end())

bool operator<(const pair<ll, ll> &a, const pair<ll, ll> &b)
{

    if (a.first == b.first)
        return a.second < b.second;

    return a.first < b.first;
}

ll gcd(ll a, ll b)
{
    if (b == 0)
        return a;
    return gcd(b, a % b);
}

int A[1010], B[1010];
int vote[1010];
int main()
{
    cin.tie(0);
    ios::sync_with_stdio(false);
    //cout << fixed << setprecision(15);

    int N, M;
    cin >> N >> M;
    for (int i = 0; i < N; ++i)
        cin >> A[i];
    for (int i = 0; i < M; ++i)
    {
        int a;
        cin >> a;

        for (int j = 0; j < N; ++j)
        {
            if (a >= A[j])
            {
                vote[j]++;
                break;
            }
        }
    }

    int maxV = 0;
    int ans = 0;

    for (int i = 0; i < N; ++i)
    {
        if (maxV < vote[i])
        {
            maxV = vote[i];
            ans = i + 1;
        }
    }
    cout << ans << endl;

    return 0;
}


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

#define int long long

#define rep(i,n) for(int i=0;i<(n);i++)
#define pb push_back
#define all(v) (v).begin(),(v).end()
#define fi first
#define se second
typedef vector<int>vint;
typedef pair<int,int>pint;
typedef vector<pint>vpint;

template<typename A,typename B>inline void chmin(A &a,B b){if(a>b)a=b;}
template<typename A,typename B>inline void chmax(A &a,B b){if(a<b)a=b;}

signed main(){
    int n,a,b;
    cin>>n>>a>>b;
    if((b-a)&1)cout<<"Borys"<<endl;
    else cout<<"Alice"<<endl;
    return 0;
}


Measured memory_bytes: 256
#include<bits/stdc++.h>
using namespace std;
int main ()
{
    int n;cin >> n;
    int A[n+10];
    for(int i=1;i<=n;i++)cin>>A[i];
    int c=0;
    for(int i=1;i<=n;i++){
        if(A[i]!=i)c++;
    }
    if(c<=2)cout<<"YES\n";
    else cout<<"NO\n";
    //main();
    return 0;
}


Measured memory_bytes: 256
#include <stdio.h>
#include <vector>
#include <iostream>
#include <string>
using namespace std;

int main() {
  int N,K;
  string S;
  cin>>N;
  cin>>S;
  cin>>K;
  char t;
  t = S[K-1];
  for (int i = 0; i < N; i++) {
    if (S[i] != t) S[i] = '*';
  }
  cout<<S<<endl;
  return 0;
}

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


x = S
cs = set()
cs.add(x)
i = 1
while 1:
    i += 1
    if x % 2 == 0:
        x //= 2
    else:
        x = x * 3 + 1
    if x in cs:
        break
    cs.add(x)

print(i)


Measured memory_bytes: 3060
import java.util.Scanner;

public class Main{
    public static void main(String[]args){
        Scanner scn = new Scanner(System.in);
        
        while(true){
            int m=scn.nextInt();
            int f=scn.nextInt();
            int r=scn.nextInt();
            
            if(m==-1 && f==-1 && r==-1){
                break;
            } else {
                transcript(m,f,r);
            }
        }
    }

    private static void transcript(int m, int f, int r) {
        int sum=m+f;
        if(m==-1 || f==-1){
            System.out.println("F");
        }else if(sum >= 80){
            System.out.println("A");
        }else if(sum>=65 && sum<80){
            System.out.println("B");
        }else if(sum>=50 && sum<65){
            System.out.println("C");
        }else if(sum>=30 && sum<50){
            if(r>=50){
                System.out.println("C");
            }else{
                System.out.println("D");
            }
        }else if(sum>=0 && sum<30){
            System.out.println("F");
        }
    }
}

Measured memory_bytes: 17428
import java.util.*;

public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int N = sc.nextInt();
		int[] cnt = new int[100000];
		for(int i=0; i<N; i++)
			cnt[sc.nextInt()-1]++;
		int single = 0;
		int multi = 0;
		for(int i=0; i<100000; i++) {
			if(cnt[i]>0) {
				if(cnt[i]%2==0)
					multi++;
				else
					single++;
			}
		}
		
		System.out.println(single + multi - (multi%2));
		sc.close();
	}
}


Measured memory_bytes: 32172
n = int(input())
V = list(map(int,input().split()))
dict_V1,dict_V2 = {},{}
if(n%2 == 0):
  len_v1,len_v2 = n//2,n//2
else:
  len_v1,len_v2 = n//2 + 1,n//2

i = 0
for v in V:
  if(i%2 == 0):
   if(str(v) in dict_V1): dict_V1[str(v)] += 1
   else: dict_V1[str(v)] = 1
  else:
   if(str(v) in dict_V2): dict_V2[str(v)] += 1
   else: dict_V2[str(v)] = 1
  i += 1
 
min_cost = 0
max_k_v1 = max(dict_V1, key=dict_V1.get)
max_k_v2 = max(dict_V2, key=dict_V2.get)

#
'''
(ex) 
V1 = [3,3,3,4],V2 = [3,3,3,2]
->(書き換え) V1 = [3,3,3,3],V2 = [3,3,3,3]
-> 文字一種類に ->NG
のケースを考慮した場合分けが必要
'''
#

if(max_k_v1 != max_k_v2):
  min_cost += len_v1 - max(dict_V1.values())
  min_cost += len_v2 - max(dict_V2.values())
else:
  min_cost1,min_cost2 = 0,0
  tmp_dict = dict_V1.copy()
  tmp_dict[max_k_v1] = 0
  min_cost1 += len_v1 - max(tmp_dict.values())
  min_cost1 += len_v2 - max(dict_V2.values())
  
  dict_V2[max_k_v1] = 0
  min_cost2 += len_v1 - max(dict_V1.values())
  min_cost2 += len_v2 - max(dict_V2.values())
  min_cost = min(min_cost1,min_cost2)
  
print(min_cost)  

Measured memory_bytes: 21800
import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.io.BufferedWriter;
import java.io.Writer;
import java.io.OutputStreamWriter;
import java.util.InputMismatchException;
import java.io.IOException;
import java.io.InputStream;

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

    static class AAdditionAndSubtractionEasy {
        public void solve(int testNumber, InputReader in, OutputWriter out) {
            String[] a = in.readLine().split(" ");
            if (a[1].equals("+")) {
                out.printLine(Integer.parseInt(a[0]) + Integer.parseInt(a[2]));
            } else {
                out.printLine(Integer.parseInt(a[0]) - Integer.parseInt(a[2]));
            }
        }

    }

    static class InputReader {
        private InputStream stream;
        private byte[] buf = new byte[1024];
        private int curChar;
        private int numChars;

        public InputReader(InputStream stream) {
            this.stream = stream;
        }

        public int read() {
            if (numChars == -1) {
                throw new InputMismatchException();
            }
            if (curChar >= numChars) {
                curChar = 0;
                try {
                    numChars = stream.read(buf);
                } catch (IOException e) {
                    throw new InputMismatchException();
                }
                if (numChars <= 0) {
                    return -1;
                }
            }
            return buf[curChar++];
        }

        private String readLine0() {
            StringBuilder buf = new StringBuilder();
            int c = read();
            while (c != '\n' && c != -1) {
                if (c != '\r') {
                    buf.appendCodePoint(c);
                }
                c = read();
            }
            return buf.toString();
        }

        public String readLine() {
            String s = readLine0();
            while (s.trim().length() == 0) {
                s = readLine0();
            }
            return s;
        }

    }

    static class OutputWriter {
        private final PrintWriter writer;

        public OutputWriter(OutputStream outputStream) {
            writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream)));
        }

        public OutputWriter(Writer writer) {
            this.writer = new PrintWriter(writer);
        }

        public void close() {
            writer.close();
        }

        public void printLine(int i) {
            writer.println(i);
        }

    }
}



Measured memory_bytes: 21460
#include<iostream>
#include<iomanip>
using namespace std;
int main(){
  double s,ans;
  while(cin>>s){
    ans = s;
    for(int i=2;i<=10;i++){
      if(i%2==0){
	s *= 2;
	ans += s;
      }
      else{
	s /= 3;
	ans += s;
      }
    }
    cout << fixed << setprecision(6) << ans << endl;
  }
  return 0;
} 

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

int main(){
    string str;
    cin >> str;

    for (int i=0; i < str.size(); ++i){
        if(str[i] == '7'){
            cout << "Yes" << endl;
            return 0;
        }
    }
    cout << "No" <<endl;
    return 0;
}

Measured memory_bytes: 3604
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
const ll mod = 1e9 + 7;
#define pival 3.14159265359
#define pll pair<ll,ll>
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define pqq priority_queue
#define all(a) a.begin(), a.end()
#define sz(a) (ll)(a.size())
#define endl '\n'
ll power(ll x, ll y, ll p)
{ 
    ll res = 1;  
    x = x % p;
    while (y > 0) { 
        if (y & 1) res = (res * x) % p; 
        y = y >> 1;
        x = (x * x) % p;   
    } 
    return res; 
}
ll n;
ll check (ll ind) {
	ll temp = n;
	while (temp % ind == 0) {
		temp /= ind;
	}
	if (temp % ind == 1) return 1;
	return 0;
}
int main()
{
	ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
 	cin >> n;
 	ll ans = 1;
 	if (n == 2) {
 		cout << 1 << endl;
 		return 0;
 	}
 	for (ll i = 2; i * i <= n; i++) {
 		if (n % i == 0) {
 			ans += check(i);
 			if (n / i != i) {
 				ans += check(n / i);
 			}
 		}
 	}   
 	set <ll> as;
 	ll tempn = n - 1;
 	as.insert(n - 1);
 	for (ll i = 2; i * i <= tempn; i++) {
 		if (tempn % i == 0) {
 			as.insert(i);
 			as.insert(tempn / i);
 		}
 	}
 	cout << ans + sz(as) << endl;
	return 0;
}

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

#A
def A():
    return

#B
def B():
    return

#C
def C():
    return

#D
def D():
    return

#E
def E():
    move = [[1, 0], [-1, 0], [0, 1], [0, -1]]
    while 1:
        w, h = LI()
        if w == h == 0:
            break
        check = defaultdict(lambda: True)
        field = SR(h)
        for i in range(h):
            for k in range(2 * w + 1):
                if field[i][k] == "L":
                    L = [k, i]
                if field[i][k] == "R":
                    R = [k, i]
        state = deque()
        state.append([L, R])
        flg = True
        while state and flg:
            l, r = state.pop()
            lx, ly = l
            rx, ry = r
            for mx, my in move:
                xl = lx + mx
                yl = ly + my
                xr = rx - mx
                yr = ry + my
                if not 0 <= xl < w:
                    xl -= mx
                if not w + 1 <= xr < 2 * w + 1:
                    xr += mx
                if not 0 <= yl < h:
                    yl -= my
                if not 0 <= yr < h:
                    yr -= my
                if field[yl][xl] == "%" and field[yr][xr] == "%":
                    flg = False
                    break
                if field[yl][xl] == "#":
                    yl -= my
                    xl -= mx
                if field[yr][xr] == "#":
                    yr -= my
                    xr += mx
                if check[(xl, xr, yl, yr)] and field[yl][xl] != "%" and field[yr][xr] != "%":
                    check[(xl, xr, yl, yr)] = False
                    state.append([[xl, yl], [xr, yr]])
        if not flg:
            print("Yes")
        else:
            print("No")
    return

#F
def F():
    return

#G
def G():
    return

#H
def H():
    return

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



Measured memory_bytes: 295804
#include <stdio.h>
#include <iostream>
#include <queue>
#include <string>
#include <algorithm>
#include <cstdio>
#include <stack>
#include <vector>
#include <string.h>
#define inf 0x7fffffff
using namespace std;
//1232
int a,b,c,m,n,l[33000333],o[3000],e[3333],k,i,d[200010],dx[10]={0,-2,-1,0,1,2};
long long x,y,z;
char iop;
stack<int> s;
struct P
{
    int x,y;
    bool operator<(const P &a)const
    {
        return y>a.y;
    }
};

string r;
vector<int> v[200010];


/*
6 16
................
..xxxx....xxx...
...xxxx....xx...
.xxxx......xxx..
........xxxxx...
..xxx....xxx....

*/
//131072


int main()
{
    scanf("%d",&a);
    for(int t=2;t<=a;t++)
        if(!o[t])
    {
        for(int w=t*t;w<=a;w+=t) o[w]=1;
        i++,l[i]=t,e[i]++;
    }

    for(int t=2;t<=a;t++)
    if(o[t])
    {
    for(int w=t,h=1;w>1;h++)
    {
        int k=0;
        for(;w%l[h]==0;k++,w/=l[h]);
        e[h]+=k;
    }

    }
    x=1;
    for(int t=1;t<=i;t++)
    {
        x=x*(e[t]+1);
        x=x%1000000007;
    }
    printf("%lld",x);
    return 0;
}

Measured memory_bytes: 4992
#pragma region Macros
#include <bits/stdc++.h>

using namespace std;
using ll = long long;
using Graph = vector<vector<int>>;
using WGraph = vector<vector<pair<int, ll>>>;

template<class T>bool chmax(T &a, const T &b) { if (b > a) { a = b; return true; } return false; }
template<class T>bool chmin(T &a, const T &b) { if (b < a) { a = b; return true; } return false; }

constexpr int dx[4] = {-1, 0, 0, 1}, dy[4] = {0, -1, 1, 0};
constexpr int INF = 1e9;
constexpr long long LINF = 1e18;
#pragma endregion

long long gcd(long long a, long long b) {
    if (a < b) std::swap(a, b);
    if (b == 0) return a;

    while (a%b > 0) {
        long long buf = b;
        b = a%b;
        a = buf;
    }
    return b;
}

void solve() {
    int n;
    ll m;
    cin >> n >> m;
    vector<ll> a(n);
    for (int i=0; i<n; i++) {
        cin >> a.at(i);
    }
    int cnt2 = 0;
    for (int i=0; i<n; i++) {
        int tmp = 0;
        while (a.at(i)%2 == 0) {
            a.at(i) /= 2;
            tmp++;
        }
        if (i == 0) cnt2 = tmp;
        else {
            if (cnt2 != tmp) {
                cout << 0 << "\n";
                return;
            }
        }
    }
    ll odd = 1;
    for (int i=0; i<n; i++) {
        odd = odd * a.at(i) / gcd(odd, a.at(i));
    }
    if (cnt2 >= 2) odd *= pow(2, cnt2-1);
    cout << (m / odd) / 2 + (m / odd) % 2 << "\n";
}

int main() {
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout << fixed << setprecision(20);

    solve();

    return 0;
}


Measured memory_bytes: 1024
while 1:
    A=[]
    B=[]
    n=int(input())
    
    if n==0:
        break

    else:
        for i in range(n):
            d=map(int,input().split(' '))
            D=list(d)


            if D[0]>D[1]:
                A.append(D[0])
                A.append(D[1])
                

            elif D[0]==D[1]:
                A.append(D[0])
                B.append(D[1])

            else:
                B.append(D[0])
                B.append(D[1])


    AP=sum(A)
    BP=sum(B)

    print(AP,BP)
                
                
                
            
    
    #print (A,B)



Measured memory_bytes: 5788
n = int(input())
h = list(map(int,input().split()))
dp = [int(1e+10) for _ in range(n)]
dp[0],dp[1] = 0,abs(h[1]-h[0])
for i in range(2,n):
    dp[i] = min(dp[i-1] + abs(h[i] - h[i-1]),dp[i-2] + abs(h[i] - h[i-2]))
print(dp[-1])

Measured memory_bytes: 13980
#include<iostream>
#include<cstdio>
#include<cstring>
#include <cstdlib>  
#include <math.h>
#include <cmath>
#include<complex>
#include<cctype>
#include<string>
#include<set>
#include<iomanip>
#include <map>
#include<algorithm>
#include <functional>
#include<vector>
#include<climits>
#include<stack>
#include<queue>
#include<bitset>
#include <deque>
#include <climits>
#include <typeinfo>
#include <utility> 
using namespace std;
using Complex = complex<double>;
using ll = long long;
template<typename T>using min_priority_queue = priority_queue<T, vector<T>, greater<T>>;
const ll inf = 1145141919810364364LL;
double PI = acos(-1.0);
#define all(x) (x).begin(),(x).end()
#define puts(x) cout << x << endl
#define rep(i,m,n) for(ll i = m;i < n;++i)
#define pb push_back
#define fore(i,a) for(auto &i:a)
#define rrep(i,m,n) for(ll i = m;i >= n;--i)

struct edge { ll to, cap, rev; };

const int MAX_V = 220;
int used[MAX_V + 3];
vector<edge>G[MAX_V + 3];

ll dfs(ll v, ll t, ll f) {
	if (v == t)return f;
	used[v] = 1;
	for (int i = 0; i < G[v].size(); i++) {
		edge e = G[v][i];
		if (used[e.to] == 0 && e.cap > 0) {
			ll d = dfs(e.to, t, min(f, e.cap));
			if (d > 0) {
				G[v][i].cap -= d;
				G[e.to][e.rev].cap += d;
				return d;
			}
		}
	}
	return 0;
}

void add_edge(ll from, ll to, ll cap) {
	edge fe = { to,cap,(int)G[to].size() };
	edge toe = { from,0,(int)G[from].size() };
	G[from].pb(fe);
	G[to].pb(toe);
}

ll max_flow(int s, int t) {
	ll flow = 0;
	while (1) {
		memset(used, 0, sizeof(used));
		ll f = dfs(s, t, inf);
		if (f == 0)return flow;
		flow += f;
	}
}

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

	int H, W;
	cin >> H >> W;
	int si, sj, ti, tj;
	for (int i = 1; i <= H; i++) {
		for (int j = 1; j <= W; j++) {
			char c;
			cin >> c;
			if (c == 'S') {
				si = i;
				sj = j;
				add_edge(0,i,inf);
				add_edge(i,0,inf);
				add_edge(0,H+j,inf);
				add_edge(H+j,0,inf);
			}
			else if(c == 'T'){
				ti = i;
				tj = j;
				add_edge(i,H+W+1,inf);
				add_edge(H+W+1,i,inf);
				add_edge(H+j,H+W+1,inf);
				add_edge(H+W+1,H+j,inf);
			}
			else if (c == 'o') {
				add_edge(i,H+j,1);
				add_edge(H+j,i,1);
			}
		}
	}
	
	if (si == ti || sj == tj)puts(-1);
	else puts(max_flow(0,H+W+1));

	return 0;
}

Measured memory_bytes: 1536
#define _CRT_SECURE_NO_WARNINGS
#pragma comment(linker, "/stack:512000000")
#include<utility>
#include<iostream>
#include<fstream>
#include<vector>
#include<array>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<ctime>
#include<functional>
#include<random>
#include<climits>
#include<queue>
#include<stack>
#include<deque>
#include<bitset>
#include<map>
#include<set>
#include<unordered_map>
#include<unordered_set>
#include<cassert>
#include<ccomplex>

#ifdef ONLINE_JUDGE
#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,tune=native")
#pragma GCC optimize("unroll-loops")
#endif

#define all(x) x.begin(), x.end()
#define mp make_pair
#define X first
#define Y second

using namespace std;

using ll = long long;
using ull = unsigned long long;
using ld = long double;
mt19937_64 gen(time(nullptr));

ll const mod = 1e9 + 7;

namespace {
	ll mul(ll a, ll b) {
		ll val = a * b - (ll)((ld)a * b / mod) * mod;
		if (val < 0) val += mod;
		if (val >= mod) val -= mod;
		return val;
	}

	ll poww(ll a, ll b) {
		ll val = 1;
		a %= mod;
		while (b > 0) {
			if (b % 2) val = mul(a, val);
			a = mul(a, a);
			b >>= 1;
		}
		return val % mod;
	}

	ll inv(ll a) {
		return poww(a, mod - 2);
	}
}

int const maxn = 3e5 + 5;

using matrix = vector<vector<ll>>;

int n;
matrix neutral;

matrix mul(matrix a, matrix b) {
	matrix c(n);
	for (int i = 0; i < n; i++) {
		c[i].resize(n);
	}
	for (int i = 0; i < n; i++) {
		for (int j = 0; j < n; j++) {
			for (int k = 0; k < n; k++) {
				c[i][j] += a[i][k] * b[k][j];
				c[i][j] %= mod;
			}
		}
	}
	return c;
}


matrix poww(matrix a, ll p) {
	if (p == 0) return neutral;
	if (p == 1) return a;
	auto c = poww(a, p / 2);
	auto cc = mul(c, c);
	if (p % 2) {
		return mul(cc, a);
	} else {
		return cc;
	}
}

signed main() {
	ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
	cin >> n;

	neutral.resize(n, vector<ll>(n));
	for (ll i = 0; i < n; i++) neutral[i][i] = 1;


	ll k; cin >> k;

	vector<vector<ll>>a(n, vector<ll>(n));

	for (ll i = 0; i < n; i++) {
		for (ll j = 0; j < n; j++) {
			cin >> a[i][j];
		}
	}

	auto kek = poww(a, k);
	ll ans = 0;
	for (ll i = 0; i < n; i++) {
		for (ll j = 0; j < n; j++) {
			ans += kek[i][j]; ans %= mod;
		}
	}
	cout << ans << endl;

	return 0;
}

/*
5
2 6 15 5 10

4
2 6 15 5

*/

Measured memory_bytes: 1664
#include "bits/stdc++.h"
using namespace std;
#ifdef _DEBUG
#include "dump.hpp"
#else
#define dump(...)
#endif

//#define int long long
#define rep(i,a,b) for(int i=(a);i<(b);i++)
#define rrep(i,a,b) for(int i=(b)-1;i>=(a);i--)
#define all(c) begin(c),end(c)
const int INF = sizeof(int) == sizeof(long long) ? 0x3f3f3f3f3f3f3f3fLL : 0x3f3f3f3f;
const int MOD = (int)(1e9) + 7;
template<class T> bool chmax(T &a, const T &b) { if (a < b) { a = b; return true; } return false; }
template<class T> bool chmin(T &a, const T &b) { if (b < a) { a = b; return true; } return false; }

struct SuffixArray {
	int n, k;
	string s;
	vector<int> ord, tmp, left;
	SuffixArray(const string &s) :s(s), n(s.size()), ord(n + 1), tmp(n + 1), left(n + 1) {
		for (int i = 0; i <= n; i++) {
			left[i] = i;
			ord[i] = i < n ? s[i] : -1;
		}
		auto compare = [&](int i, int j) {
			if (ord[i] != ord[j])return ord[i] < ord[j];
			else {
				int ri = i + k <= n ? ord[i + k] : -1;
				int rj = j + k <= n ? ord[j + k] : -1;
				return ri < rj;
			}
		};
		for (k = 1; k <= n; k *= 2) {
			sort(left.begin(), left.end(), compare);
			tmp[left[0]] = 0;
			for (int i = 1; i <= n; i++) {
				tmp[left[i]] = tmp[left[i - 1]] + (compare(left[i - 1], left[i]) ? 1 : 0);
			}
			ord = tmp;
		}
	}
	int lowerBound(const string &pattern) {
		int ng = 0, ok = n + 1;
		while (ng + 1 < ok) {
			int m = (ng + ok) / 2;
			if (s.compare(left[m], pattern.size(), pattern) >= 0)
				ok = m;
			else
				ng = m;
		}
		return ok;
	}
	int search(const string &pattern) {
		int ok = lowerBound(pattern);
		if (ok == n + 1)
			return -1;
		else
			return s.compare(left[ok], pattern.size(), pattern) == 0 ? ok : -1;
	}
};

struct RangeMinimumQuery {
	int n;
	vector<int> d;
	void init(int m) {
		for (n = 1; n < m; n *= 2);
		d.assign(n * 2, INF);
	}
	void update(int i, int x) {
		d[n + i] = x;
		for (int j = (n + i) / 2; j > 0; j >>= 1)
			d[j] = min(d[j * 2], d[j * 2 + 1]);
	}
	//[l, r)
	int query(int l, int r) {
		int mi = get(l);
		for (; l && l + (l&-l) <= r; l += l&-l)
			mi = min(mi, d[(n + l) / (l&-l)]);
		for (; l < r; r -= r&-r)
			mi = min(mi, d[(n + r) / (r&-r) - 1]);
		return mi;
	}
	int get(int i) { return d[n + i]; }
};

signed main() {
	cin.tie(0);
	ios::sync_with_stdio(false);
	string s; cin >> s;
	string t = s; reverse(all(t));
	SuffixArray sas(s), sat(t);
	int n = s.size();
	RangeMinimumQuery rmqs, rmqr;
	rmqs.init(n + 1); rmqr.init(n + 1);
	rep(i, 0, n + 1) {
		rmqs.update(i, sas.left[i]);
		rmqr.update(i, sat.left[i]);
	}
	int m; cin >> m;
	rep(i, 0, m) {
		string x, y; cin >> x >> y;
		dump(x, y);
		reverse(all(y));
		int l, r, L, R;
		l = sas.search(x); x.back()++; r = sas.lowerBound(x);
		dump(l, r);
		if (l == -1) {
			cout << 0 << endl;
			continue;
		}
		L = rmqs.query(l, r);
		l = sat.search(y); y.back()++; r = sat.lowerBound(y);
		dump(l, r);
		if (l == -1) {
			cout << 0 << endl;
			continue;
		}
		R = rmqr.query(l, r);
		int ans = 0;
		dump(L, R);
		if (L != INF && R != INF) {
			R = n - R;
			dump(L, R);
			if (L + x.size() <= R)
				ans = R - L;
		}
		cout << ans << endl;
	}
	return 0;
}

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

int main()
{
    int n, v;
    stack<int> S;
    cin >> n;
    for (int i = 0; i != n; ++i)
    {
        cin >> v;
        S.push(v);
    }
    for (int i = 0; i != n; ++i)
    {
        if (i) cout << " ";
        cout << S.top();
        S.pop();
    }
    cout << endl;
    return 0;
}



Measured memory_bytes: 3040
import java.util.*;

class Main{
	public static void main(String[] args){
		Scanner sc = new Scanner(System.in);
		int N = sc.nextInt();
		
		if(N % 2 == 0){
			System.out.println(N / 2);
		}else{
			System.out.println(++N / 2);
		}
	}
}

Measured memory_bytes: 21844
#include<iostream>//aoj631
using namespace std;
int main(void)
{
	int i,n,m,a,b,c=0,d=0,e=0;
	cin>>n>>m;
	for(i=1;i<=m;i++){
		cin>>a>>b;
		if(a>=n) c=0;
		if(a<n) c=n-a;
		if(d<c) d=c;
		e=e+c;
	}
	e=e-d;
	cout<<e<<endl;
	return 0;
}


Measured memory_bytes: 3096
a = input().split()
b = 3
if a[0] == a[1] and a[0] == a[2] and a[1] == a[2]:
    b = 1
elif a[0] != a[1] and a[0] == a[2] and a[1] != a[2]:
    b = 2
elif a[0] == a[1] and a[0] != a[2] and a[1] != a[2]:
    b = 2
elif a[0] != a[1] and a[0] != a[2] and a[1] == a[2]:
    b = 2
elif a[0] != a[1] and a[0] != a[2] and a[1] != a[2]:
    b = 3
print(b)

Measured memory_bytes: 3064
n=int(input())
tl=[]
for inp in range(n):
    ab = input().split()
    tl.append((int(ab[1]),int(ab[0])))

#print(tl)

tl.sort()

#print(tl)

sums=0
for tup in tl:
    sums+=tup[1]
    if tup[0]<sums:
        #print(sums,tup[0])
        ans="No"
        break
else:
    ans="Yes"

print(ans)

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

int main(){
  ll a,b,c,d;
  cin >> a >> b >> c >> d;
  while(true){
    c -= b;
    if( c <= 0 ){
      cout << "Yes" << endl;
      return 0;
    }
    a -= d;
    if( a <= 0 ){
      cout << "No" << endl;
      return 0;
    }
  }
  return 0;
}

Measured memory_bytes: 3596