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

const int MOD = 1000000007;

long long dp[101][100001];

int main()
{
    int n, k;
    cin >> n >> k;
    dp[0][k] = 1;
    for (int i = 0; i != n; ++i)
    {
        int x;
        scanf("%d", &x);
        
        for (int m = 0; m <= k; ++m)
        {
            if (m)
            {
                dp[i + 1][m] = (dp[i][min(k, m + x)] - dp[i][m - 1] + dp[i + 1][m - 1]) % MOD;
                if (dp[i + 1][m] < 0) dp[i + 1][m] += MOD;
            }
            else
                dp[i + 1][m] = dp[i][min(k, m + x)];
        }
    }
    
    cout << dp[n][0];
    
    return 0;
}

Measured memory_bytes: 78336
#include<bits/stdc++.h>
using namespace std;
#define int long long
signed main(){
  int n,p,a;
  char s;
  string q;
  cin>>n;
  while(true){
    if(n<=0) break;
    s='a';
    a=n%26;
    if(a==0) a=26;
    a--;
    s+=a;
    n=(n-1)/26;
    q+=s;
  }
  for(int i=q.size()-1;i>=0;i--){
    cout<<q[i];
  }
  cout<<"\n";
}

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

public class Main {
    public static void main(String[] args) {
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        try{
            String line = reader.readLine();
            char[] ch = new char[1200];
            for(int i = 0; i < line.length();i++){
                ch[i] = line.charAt(i);
                if(ch[i] >= 'A' && ch[i] <= 'Z'){
                    ch[i] += 32;
                }else if(ch[i] >= 'a' && ch[i] <= 'z'){
                    ch[i] -= 32;
                }
                System.out.print(ch[i]);
            }
          System.out.println();
        }catch(IOException e){

        }
    }
}


Measured memory_bytes: 25040
N = int(input())
t = []
x = []
y = []
for i in range(N):
  a,b,c = map(int, input().split())
  t.append(a)
  x.append(b)
  y.append(c)

def helper():
  curt = 0
  curx = 0
  cury = 0
  for i in range(N):
    dt = t[i] - curt
    curt = t[i]

    dx = abs(x[i] - curx)
    curx = x[i]

    dy = abs(y[i] - cury)
    cury = y[i]

    if dt < (dx+dy) or dt%2 != (dx+dy)%2:
      return False
  return True

a = helper()  
print('Yes') if a else print('No')


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

typedef long long       ll;

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

    return gcd(b, a%b);
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    
    int n;
    cin >> n;

    ll sum=0;
    for (int i=1; i<=n; ++i) {
        for (int j=1; j<=n; ++j) {
            for (int k=1; k<=n; ++k) {
                sum+=gcd(gcd(i, j), k);
            }
        }
    }

    cout << sum;

    return 0;
}


Measured memory_bytes: 3600
fun main(arg: Array<String>) {
    val N = nextInt()
    val H = listOfInt()
    var h = Int.MAX_VALUE
    for (n in (0 until N).reversed()) {
        if (H[n] <= h) h = H[n]
        else if (H[n] - 1 == h) h = H[n] - 1
        else {
            println("No")
            return
        }
    }
    println("Yes")
}
fun next() = readLine()!!
fun nextInt() = Integer.parseInt(next())
fun listOfString() = next().split(" ")
fun listOfInt(delta: Int = 0) = listOfString().map { Integer.parseInt(it) + delta }


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

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

    static class BComparingStrings {
        public void solve(int testNumber, Scanner in, PrintWriter out) {
            int a = in.nextInt();
            int b = in.nextInt();

            StringBuilder ar = new StringBuilder();
            StringBuilder br = new StringBuilder();

            for (int i = 0; i < b; i++) {
                ar.append(a);
            }

            for (int j = 0; j < a; j++) {
                br.append(b);
            }

            int ans = ar.toString().compareTo(br.toString());

            if (ans > 0) out.println(br);
            else out.println(ar);
        }

    }
}



Measured memory_bytes: 21844
#N人の参加者 全員がＫポイント持っている 
#Q回の正解
#Aは誰が正解したか
N,K,Q=map(int,input().split())
Ans=[]
for _ in range(Q):
    hoge=int(input())
    Ans.append(hoge-1)
# print(Ans) 
person=N*[K-Q]
# print(person)

#問題
for i in range(Q):
    person[Ans[i]]+=1
    # #人
    # for j in range(N):
    #     if Ans[i]==j:
    #         pass
    #     else:
    #         person[j]-=1
    # print(person)
# print(person)

for i in range(N):
    if person[i]>0:
        print("Yes")
    else:
        print("No")

Measured memory_bytes: 10444
#pragma GCC optimize ("O3")
#pragma GCC target ("avx")
//#include<bits/stdc++.h>
#include<cstdio>
#include<algorithm>
using namespace std;
typedef long long ll;
#define rep(i, n) for(int i = 0; i < (n); i++)
#define rep1(i, n) for(int i = 1; i <= (n); i++)
#define co(x) cout << (x) << "\n"
#define cosp(x) cout << (x) << " "
#define ce(x) cerr << (x) << "\n"
#define cesp(x) cerr << (x) << " "
#define pb push_back
#define mp make_pair
#define chmin(x, y) x = min(x, y)
#define chmax(x, y) x = max(x, y)
#define Would
#define you
#define please

const int cm = 1 << 17;
char cn[cm], * ci = cn + cm, ct;
inline char getcha() {
	if (ci - cn == cm) { fread_unlocked(cn, 1, cm, stdin); ci = cn; }
	return *ci++;
}
inline int getint() {
	int A = 0;
	if (ci - cn + 16 > cm) while ((ct = getcha()) >= '0') A = A * 10 + ct - '0';
	else while ((ct = *ci++) >= '0') A = A * 10 + ct - '0';
	return A;
}

int D[100001];
int to[200001], ne[200001], he[100001];
void dfsT(int A, int d) {
	D[A] = d;
	for (int t = he[A]; t; t = ne[t]) {
		if (!D[to[t]]) dfsT(to[t], d + 1);
	}
}

int saidai = -1;
void dfsA(int A, int d) {
	if (D[A] < d) chmax(saidai, d);
	D[A] = 0;
	for (int t = he[A]; t; t = ne[t]) {
		if (D[to[t]]) dfsA(to[t], d + 1);
	}
}

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


	int N = getint();
	int u = getint(), v = getint();
	int k = 1;
	rep(i, N - 1) {
		int a = getint(), b = getint();
		to[k] = b;
		ne[k] = he[a];
		he[a] = k++;
		to[k] = a;
		ne[k] = he[b];
		he[b] = k++;
	}
	dfsT(u, 1);
	dfsA(v, 1);
	if (saidai - 2 <= 0) printf("0");
	else printf("%d", saidai - 2);

	Would you please return 0;
}

Measured memory_bytes: 3968
nk = input().split()
n , k = int(nk[0]), int(nk[1])
a = [int(i) for i in input().split()]

t = 1
tl = [1]
ts = {1}


for i in range(len(a)):
    
    t = a[t-1]
    if t in ts:
        break
    else:
        tl.append(t)
        ts.add(t)

tl2 = tl[tl.index(t):len(tl)]
         
# print(tl)
# print(tl2)

ans = 0

if k < len(tl):
    ans = tl[k]
else:
    k = k - len(tl)
    j = k % len(tl2)
    ans = tl2[j]

print(ans)

Measured memory_bytes: 32332
#include <bits/stdc++.h>
#define rep(i,n) for (int i = (0); i < (n); ++i)
#define rrep(i,n) for(int i = 1; i <= (n); ++i)
#define drep(i,n) for(int i = (n)-1; i >= 0; --i)
#define srep(i,s,t) for (int i = s; i < t; ++i)
#define rng(x) (x).begin(),(x).end()
#define rrng(x) (x).rbegin(),(x).rend()
#define limit(x,l,r) max(l,min(x,r))
#define lims(x,l,r) (x = max(l,min(x,r)))
#define isin(x,l,r) ((l) <= (x) && (x) < (r))
#define show(x) cout << #x << " = " << (x) << endl
#define show2(x,y) cout << #x << " = " << (x) << ", " << #y << " = " << (y) << endl
#define show3(x,y,z) cout << #x << " = " << (x) << ", " << #y << " = " << (y) <<  ", " << #z << " = " << (z) << endl
#define showv(v) rep(i,v.size()) printf("%d%c", v[i], i==v.size()-1?'\n':' ')
#define showv2(v) rep(j,v.size()) showv(v[j])
#define showt(t,n) rep(i,n) printf("%d%c", t[i], i==n-1?'\n':' ')
#define showt2(t,r,c) rep(j,r) showt(t[j],c)
#define showvp(p) rep(i,p.size()) printf("%d %d\n", p[i].first, p[i].second);
#define printv(v) rep(i,v.size()) printf("%d\n", v[i])
#define printt(t,n) rep(i,n) printf("%d\n", t[i])
#define incl(v,x) find(rng(v),x)!=v.end()
#define fi first
#define se second
#define pb push_back
#define eb emplace_back
#define sz(x) (int)(x).size()
#define pcnt __builtin_popcountll
#define bn(x) ((1<<x)-1)
#define dup(x,y) (((x)+(y)-1)/(y))
#define newline puts("")
#define uni(x) x.erase(unique(rng(x)),x.end())
#define SP << " " <<
using namespace std;
using ll = long long;
using vi = vector<int>;
using vvi = vector<vi>;
using vll = vector<ll>;
using pii = pair<int, int>;
using tiii = tuple<int, int, int>;
typedef vector<pii> vp;
typedef vector<tiii> vt;
const int mod = 1000000007;
const double EPS = 1e-9;
const int INF = (1<<30)-1;
const ll INFLL = (1LL<<62)-1;
#define dame { puts("No"); return 0;}
#define yn {puts("Yes");}else{puts("No");}
inline int in() { int x; scanf("%d",&x); return x;}
//cout << fixed << setprecision(10);
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; }

int main () {
  int n, m, c;
  cin >> n >> m >> c;
  vi b(m);
  rep(i,m) cin >> b[i];
  vvi a(n,vi(m));
  rep(i,n)rep(j,m) cin >> a[i][j];

  int ans = 0;
  rep(i,n) {
    int sum = 0;
    rep(j,m) {
      sum += a[i][j]*b[j];
    }
    if (sum + c > 0) ans++;
  }

  cout << ans << endl;
  return 0;
}

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

using namespace std;

typedef long long ll;

const int N = 111;

int a[N], b[N];
int n;

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

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

    bool ok = false;

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

        bool cur_ok = true;

        for (int j = i; j < i + 3; ++j) {
            cur_ok &= a[j] == b[j];
        }

        ok |= cur_ok;

    }

    cout << (ok ? "Yes" : "No") << endl;

    return 0;
}


Measured memory_bytes: 3612
// clang-format off
#include <bits/stdc++.h>
#define int long long
#define main signed main()
// #define main int main()
#define loop(i, a, n) for (int i = (a); i < (n); i++)
#define rep(i, n) loop(i, 0, n)
#define all(v) (v).begin(), (v).end()
#define rall(v) (v).rbegin(), (v).rend()
#define prec(n) fixed << setprecision(n)
#define stlice(from, to) substr(from, (to) - (from) + 1)
#define pb push_back
#define mp make_pair
#define mt make_tuple
#define fi first
#define se second
using namespace std;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<double> vd;
typedef vector<char> vc;
typedef vector<bool> vb;
typedef vector<string> vs;
typedef vector<pii> vpii;
typedef vector<vi> vvi;
typedef vector<vb> vvb;
typedef vector<vpii> vvpii;
const int INF = sizeof(int) == sizeof(long long) ? 1000000000000000000LL : 1000000000;
const int MOD = 1000000007;
const double PI = acos(-1);
template<typename A, typename B> bool cmin(A &a, const B &b) { return a > b ? (a = b, true) : false; }
template<typename A, typename B> bool cmax(A &a, const B &b) { return a < b ? (a = b, true) : false; }
bool odd(const int &n) { return n & 1; }
bool even(const int &n) { return !odd(n); }
template<typename V> typename V::value_type sum(const V &v) { return accumulate(all(v), 0); }
void solve();
main { solve(); return 0; }
// clang-format on

void solve() {
  int x, y;
  while (cin >> x >> y, x) {
    int t = 0;
    if (x < y) swap(x, y);
    for (; y; t++) {
      x %= y;
      swap(x, y);
    }
    cout << x << ' ' << t << endl;
  }
}

Measured memory_bytes: 3040
A,B,K = map(int,input().split())
if (A - K) <= 0 and 0 < B - K + A :
    B = B - K + A
    print(0,B)
elif (A - K) <= 0 and B - K + A <= 0:
    print(0,0)
elif 0 < (A - K):
    print(A-K,B)

Measured memory_bytes: 3060
#include <iostream>
#include <map>
#include <string>

using namespace std;

void solve()
{
	int a;
	string s;
	map<string, int> Map;
	while(~scanf("%d,", &a))
	{
		cin >> s;
		++Map[s];
	}
	cout << Map["A"] << endl;
	cout << Map["B"] << endl;
	cout << Map["AB"] << endl;
	cout << Map["O"] << endl;
}

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

Measured memory_bytes: 0
#include <bits/stdc++.h>
#include <algorithm>
#define rep(i, n) for (int i=0; i<n; ++i)
#define all(obj) (obj).begin(),(obj).end()
using namespace std;
typedef long long ll;
long long GCD(long long x, long long y) { return y ? GCD(y, x%y) : x; }

int main(){
    int N;
    cin >> N;
    vector<pair<int, int>> job(N);
    rep(i, N){
        int A, B;
        cin >> A >> B;
        job[i] = make_pair(B, A);
    }
    sort(job.begin(), job.end());
    int tim = 0;
    for(auto aaa: job){
        tim+=aaa.second;
        if(tim>aaa.first){
            cout << "No" << endl;
            return 0;
        }
    }
    cout << "Yes" << endl;
    return 0;
}


Measured memory_bytes: 4828
#include <bits/stdc++.h>
using namespace std;
#define WHOLE(v) (v).begin(), (v).end()
#define REV_WHOLE(v) (v).rbegin(), (v).rend()
using i64 = int64_t;
template <size_t I, class H, class... T> struct TupleReaderWriter{static tuple<H,T...> r(istream&i){H v;i>>v;return tuple_cat(tuple<H>(v),TupleReaderWriter<sizeof...(T)-1,T...>::r(i));}static void w(ostream&o,tuple<H,T...>&t,string d){TupleReaderWriter<I-1,H,T...>::w(o,t,d);o<<d<<get<I>(t);}};
template <class H, class... T> struct TupleReaderWriter<0,H,T...>{static tuple<H,T...> r(istream&i){H v;i>>v;return tuple<H>(v);}static void w(ostream&o,tuple<H,T...>&t,string d){o<<get<0>(t);}};
template<class T>istream&operator>>(istream&i,vector<T>&v){for(auto&x:v)i>>x;return i;}
template<class T>ostream&operator<<(ostream&o,vector<T>&v){string delim="";for(auto&x:v)o<<delim<<x,delim=" ";return o;}
template <class... T> istream& operator>>(istream&i,tuple<T...>& t) { t = TupleReaderWriter<sizeof...(T), T...>::r(i); return i; }
template <class... T> ostream& operator<<(ostream& o, tuple<T...>& t) { string delim = " "; TupleReaderWriter<sizeof...(T) - 1, T...>::w(o, t, delim); return o; }
template<typename T> using rev_priority_queue = priority_queue<T, vector<T>, greater<T>>;
template<typename T> using vector2d = vector<vector<T>>;
void R_YESNO(bool p) { cout << (p ? "YES" : "NO") << endl; }
void R_YesNo(bool p) { cout << (p ? "Yes" : "No") << endl; }
// 選ばれた列は無いものとして扱っても問題ない
int main() {
    const i64 INF = 1e16;
    int N, K;
    cin >> N >> K;
    vector<i64> H;
    H.push_back(0);
    for(int i = 0; i < N; i++) {
        i64 h;
        cin >> h;
        H.push_back(h);
    }
    // ダミー要素
    H.push_back(0);
    N += 2;
    // (i, k) -> x
    // k個の列を選んで除いて、一番右にあるものがiである場合の操作回数
    vector2d<i64> dp(N + 1, vector<i64>(K + 1, INF));
    dp[0][0] = 0;
    for(int i = 0; i <= N; i++) {
        for(int k = 0; k <= K; k++) {
            for(int j = 0; j < min(i, k + 1); j++) {
                dp[i][k] = min(dp[i][k],
                    dp[i - j - 1][k - j] + max<i64>(H[i] - H[i - j - 1], 0));
            }
        }
    }
    i64 ans = INF;
    for(int k = 0; k <= K; k++) {
        ans = min(ans, dp[N][k]);
    }
    cout << ans << endl;
    return 0;
}

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

void solve(){
      int k,n;
      cin>>k>>n;
      vector<int>v(n);
      for(int i=0;i<n;i++){
          cin>>v[i];
      }
      sort(v.begin(),v.end());
      int sum=v[0]+(k-v[v.size()-1]);
      int mn=v[v.size()-1]-v[0];
      for(int i=1;i<v.size();i++){
          mn=min(k-v[i]+v[i-1],mn);
      }
      cout<<mn;
}
int main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    solve();    
    return 0;
}

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

#define pb push_back
#define rep(i,n) for(int i=0;i<(n);i++)
#define reps(i,n,s) for(int i=(s);i<(n);i++)
#define rrep(i,n) for(int i=(n-1);i>=0;i--)
#define rreps(i,n,s) for(int i=s;i>=n;i--)
template<class T> bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T> bool chmin(T &a, const T &b) { if (a>b) { a=b; return 1; } return 0; }

using ll = long long;
using namespace std;
constexpr long long MAX = 5100000;
constexpr long long INF = 1LL << 60;
constexpr int MOD = 1000000007;

int main(){
cin.tie(0);
ios::sync_with_stdio(false);
int R,G,B,N;
cin>>R>>G>>B>>N;
ll ans = 0;
int nr=N/R;
int ng=N/G;
rep(r,nr+1){
    rep(g,ng+1){
        int nokori = N-(r*R+G*g);
        if(nokori>=0&&nokori%B==0){
            ans++;
        }
    }
}
cout<<ans<<endl;
return 0;
}

Measured memory_bytes: 256
#include <iostream>
#include <algorithm>
#include <string>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <vector>
#include <queue>
#include <stack>
#include <cmath>
#include <map>
#include <iomanip>
#include <set>
using namespace std;

#define rep(i, s) for (int i = 0; i < s; ++i)
#define all(v) (v.begin(), v.end())
#define COUT(x) cout << #x << " = " << (x) << " (L" << __LINE__ << ")" << endl
#define EACH(i, s) for (__typeof__((s).begin()) i = (s).begin(); i != (s).end(); ++i)
#define VEC(a, n) vector<int>a(n)
#define PQ(a) priority_queue<int>a
#define PQmin(a) priority_queue< int, :vector<int>, greater<int> >a
#define PAIR pair<int, int>
/*
sort(sp.begin(), sp.end(), [](PAIR l, PAIR r){
        return l.first<r.first || (l.first==r.first && l.second > r.second);
        });
*/

typedef long long  ll;

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

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



int main() {
    int n;
    cin>>n;
    vector<int>a(n);
    int ans=0;
    bool flag = false;
    for(int i=0;i<n;i++){
        cin>>a.at(i);
        ans+=a.at(i);
        if(a.at(i)%10!=0){
            flag=true;
        }
    }
    sort(a.begin(),a.end());
    if(ans%10==0){
        for(int i=0;i<n;i++){
            if(a.at(i)%10!=0){
                ans-=a.at(i);
                break;
            }
        }
    }
    if(!flag){
        cout<<0<<endl;
    }
    else{
        cout<<ans<<endl;
    }






    return 0;
}

Measured memory_bytes: 256
N = int(input())
A = [0] + list(map(int, input().split()))

for i in range(N//2, 0, -1):
    tmp = 0
    j = i
    while j < N+1:
        tmp += A[j]
        j += i
    A[i] = tmp % 2

ans = []
for i, j in enumerate(A):
    if j == 1:
        ans.append(i)
# ans = [i for i, j in enumerate(A) if j == 1]

print(len(ans))
print(*ans)

Measured memory_bytes: 12596
n = gets.to_i
t, a = gets.chomp.split.map(&:to_i)
hs = gets.chomp.split.map(&:to_i) 

hsAvg = []
hs.map {|n| hsAvg << t - (n * 0.006)}

diff = []
hsAvg.map {|n| diff << (a - n).abs}

p diff.index(diff.min) + 1



Measured memory_bytes: 1788
#include <bits/stdc++.h>
using namespace std;
int main(){
  int n,m;
  cin >> n >> m;
  vector<bool> p(n);
  vector<bool> q(n);
  for(int i = 0;i < m;i++){
    int a,b;
    cin >> a >> b;
    a--;
    b--;
    if(a == 0)p[b] = true;
    else if(b == n-1)q[a] = true;
  }
  for(int i = 1;i < n;i++){
    if(i == n-1)cout << "IMPOSSIBLE" << endl;
    else if(p[i] == true && q[i] == true){
      cout << "POSSIBLE" << endl;
      break;
    }
  }
}


Measured memory_bytes: 256
#include <bits/stdc++.h>
#define rep(i,n) for(int i=0;i<(n);++i)
#define rrep(i,n) for(int i=1;i<(n);++i)
#define all(a) (a).begin(),(a).end()
#define rall(a) (a).rbegin(),(a).rend()
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
const int inf = 1001001001;
const int mod = 1000000007;

struct mint {
	ll x;
	mint() :x(0) {}
	mint(ll x) :x((x% mod + mod) % mod) {}
	// mint(ll x):x(x){}
	mint& fix() { x = (x % mod + mod) % mod; return *this; }
	mint operator-() const { return mint(0) - *this; }
	mint operator~() const { return mint(1) / *this; }
	mint& operator+=(const mint& a) { if ((x += a.x) >= mod) x -= mod; return *this; }
	mint& operator-=(const mint& a) { if ((x += mod - a.x) >= mod) x -= mod; return *this; }
	mint& operator*=(const mint& a) { (x *= a.x) %= mod; return *this; }
	mint& operator/=(const mint& a) { (x *= a.pow(mod - 2).x) %= mod; return *this; }
	mint operator+(const mint& a)const { return mint(*this) += a; }
	mint operator-(const mint& a)const { return mint(*this) -= a; }
	mint operator*(const mint& a)const { return mint(*this) *= a; }
	mint operator/(const mint& a)const { return mint(*this) /= a; }
	mint pow(ll t) const {
		if (!t) return 1;
		mint res = pow(t / 2);
		res *= res;
		return (t & 1) ? res * x : res;
	}
	bool operator<(const mint& a)const { return x < a.x; }
	bool operator==(const mint& a)const { return x == a.x; }
};

int main() {
	ios::sync_with_stdio(false);
	cin.tie(0);
	ll n, K;
	cin >> n >> K;
	vector<int> a(n);
	rep(i, n) cin >> a[i];
	mint ans = 0;
	mint k = K;
	rep(i, n) {
		mint cnt = 0;
		mint cnt2 = 0;
		rep(j, n) {
			if (a[j] > a[i]) cnt += 1;
		}
		for (int j = 0; j < i; ++j) {
			if (a[j] > a[i]) cnt2 += 1;
		}
		ans += (k - 1) * k / 2 * cnt;
		ans += k * cnt2;
	}
	cout << ans.x << endl;
	return 0;
}


Measured memory_bytes: 256
from collections import Counter
n=int(input())
s=[input() for i in range(n)]
c1=Counter(s.pop())

for ss in s:
    c2=Counter(ss)
    for k in c1.keys():
        c1[k]=min(c1[k],c2[k])

ans=''
for k,v in sorted(c1.items()):
    ans+=k*v
print(ans)




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

#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
using ll = long long;

int main(int argc, char const *argv[])
{
    int k, x;
    cin >> k >> x;
    if (500 * k >= x)
        cout << "Yes";
    else
        cout << "No";
    return 0;
}

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

using namespace std;

#define ll long long

ll k, a, b, it, ans, t, x, mem;

int main(){
    cin >> k >> a >> b;
    if (a >= b || k <= a || b - a == 1){
        ans = 1 + k;
    }
    else{
        mem = k;
        k -= (a + 1);
        t = k / 2;

        it = (b - a) * t + b;
        //cout << it << endl;

        if (k % 2 != 0){
            it++;
        }
        ans = max(it, mem + 1);
    }

    cout << ans;
}


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

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

int main(){
  int n;
  std::cin >> n;
  int b[101];
  int sum=0;
  int c[101];
  for (int i = 0; i < n-1; i++) {
    std::cin >> b[i];
  }

  c[0]=b[0];
  c[1]=b[0];
  for (int i =1; i <n-1; i++) {
    if(c[i]>b[i])c[i]=b[i];
    c[i+1]=b[i];
  }
  for (int i = 0; i < n; i++) {
    sum+=c[i];
  }
  std::cout << sum << '\n';


  return 0;
}


Measured memory_bytes: 256
#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <iostream>
#include <istream>
#include <iterator>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
#include <tuple>
#include <iomanip>
#include <climits>
using namespace std;

typedef long long ll;
typedef unsigned long long ull;
//typedef pair<ll, ll> P;
#define rep(i, n) for(ll i = 0; i < (n); i++)
#define revrep(i, n) for(ll i = (n)-1; i >= 0; i--)
#define pb push_back
#define f first
#define s second
#define chmin(x, y) x = min(x, y);
#define chmax(x, y) x = max(x, y);

//const ll INFL = LLONG_MAX;//10^18 = 2^60
const ll INFL = 1LL<<60;
//const int INF = INT_MAX;
const ll INF = 1 << 30;//10^9
ll MOD = 1000000007;
//ll MOD = 998244353;

vector<ll> dy = {0, 1, 0, -1, 1, 1, -1, -1, 0};
vector<ll> dx = {1, 0, -1, 0, 1, -1, 1, -1, 0};

void pres(double A, ll x = 10){cout << fixed << setprecision(x) << A << endl;}
void BinarySay(ll x, ll y = 60){rep(i, y) cout << (x>>(y-1-i) & 1); cout << endl;}
ll cnt_bit(ll x){return __builtin_popcountll(x);}

ll pow_long(ll x, ll k){
  ll res = 1;
  while(k > 0){
    if(k % 2) res *= x;
    x *= x; k /= 2;
  }
  return res;
}

ll pow_mod(ll x, ll k){
  ll res = 1;
  while(k > 0){
    if(k % 2){
      res *= x; res %= MOD;
    }
    x *= x; x %= MOD;
    k /= 2;
  }
  return res;
}

ll inverse(ll x){return pow_mod(x, MOD - 2);};

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

ll lcm(ll x, ll y){return x / gcd(x, y) * y;};



void solve(){
  int N;
  cin >> N;
  string S;
  cin >> S;
  if(S.size() % 2) cout << "No" << endl;
  else{
    for(int i = 0; i < S.size(); i++){
      if(S[i] != S[(i+S.size()/2)%S.size()]){
        cout << "No" << endl;
        return;
      }
    }
    cout << "Yes" << endl;
  }
}

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


Measured memory_bytes: 256
#include <bits/stdc++.h>
using namespace std;
int main(){
    long long n;
    cin>>n;
    long long a[n], ans=0;
    for(long long i=1;i<=n;i++){
        if(i%3==0||i%5==0){
            a[i-1]=0;
        }else{
            a[i-1]=i;
        }ans+=a[i-1];
    }cout<<ans<<endl;
	return 0;
}

Measured memory_bytes: 11368
#include<bits/stdc++.h>
using namespace std;
int n,t,x,y;
int main()
{
    cin>>n;
    for(int i=0;i<n;i++)
    {
        cin>>t>>x>>y;
        if(!(x+y<=t&&(t-x-y)%2==0))
        {
            cout<<"No\n";
            return 0;
        }
    }
    cout<<"Yes\n";
}



Measured memory_bytes: 256
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i,n) for(int i=0;i<n;i++)
int main(void){
    // Your code here!
    vector<string> YN{"NO","YES"};
    vector<string> yn{"No","Yes"};
    int a,b,c;
    cin>>a>>b;
    cout<<(!(a%3)||!(b%3)||!((a+b)%3)?"Possible":"Impossible")<<endl;
}


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

int main() {
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    int a, b, m; cin >> a >> b >> m;
    vector<int> x(a), y(b);
    for (int i = 0; i < a; i++) cin >> x[i];
    for (int j = 0; j < b; j++) cin >> y[j];
    int ans = 1e9;
    for (int i = 0; i < m; i++) {
        int p, q, r; cin >> p >> q >> r;
        ans = min(ans, x[p-1] + y[q-1] - r);
    }
    sort(x.begin(), x.end());
    sort(y.begin(), y.end());
    ans = min(ans, x[0] + y[0]);
    cout << ans << "\n";
    return 0;
}

Measured memory_bytes: 1024
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
using namespace std;
long n,a[1000005],f[1000005],k;
int main() {
    cin >> n  >> k;
    for (long i =1; i<=n; i++) cin >> a[i];
    f[1] = 0;
    f[2] = abs(a[1] - a[2]);
    for (long i=3; i<=n; i++) {
        f[i] = 9999999999;
        for (long j=1; j<=k;j++) {
            if (i - j <= 0) break;
            f[i] = min(f[i],f[i-j] + abs(a[i] - a[i-j]));
        }
    }
    cout << f[n];
    return 0;
}


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

int SelectionSort(std::vector<int>& arr)
{
	int count = 0;
	int min;
	for (auto i = 0; i < arr.size(); ++i)
	{
		min = i;
		for (auto j = i; j < arr.size(); ++j)
			if (arr[j] < arr[min])
				min = j;
		if (i != min)
			++count;
		std::swap(arr[i], arr[min]);
	}
	return count;
}

template<typename T>
T Input()
{
	T value;
	std::cin >> value;
	return value;
}

int main()
{
	int num, count;
	std::vector<int> arr;
	std::cin >> num;
	for (auto i = 0; i < num; ++i)
		arr.push_back(Input<int>());

	count = SelectionSort(arr);

	for (auto i = 0; i < num; ++i)
		std::cout << arr[i] << (i != num - 1 ? " " : "");
	std::cout << "\n" << count << "\n";

	return 0;
}

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

int main() {
	while(true) {
		int a,b;
		cin >> a>>b;
		if (a == 0 && b == 0) break;
		if (a > b) {
			int c = a;
			a = b;
			b = c;
		}
		cout << a << " " << b << endl;
	}
	return 0;
}

Measured memory_bytes: 3076
#include<iostream>
#include<sstream>
#include<algorithm>
#include<numeric>
#include<vector>
#include<map>
#include<set>
#include<queue>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cassert>

#define rep(i,n) for(int i=0;i<n;i++)
#define all(c) (c).begin(),(c).end()
#define fr(i,c) for(__typeof((c).begin()) i=(c).begin();i!=(c).end();i++)
#define mp make_pair
#define pb push_back
#define dbg(x) cerr<<#x<<" = "<<(x)<<endl

using namespace std;

typedef long long ll;
typedef vector<int> vi;
typedef pair<int,int> pi;

const int inf=1<<28;
const double INF=1e12,EPS=1e-9;

int main()
{
  int n,m,r,q;
  while(cin>>n>>m,n){
    cin>>r;
    //cerr<<r<<endl;
    map<int,int> use[m+1];
    rep(i,r){
      int t,n1,m1,s; cin>>t>>n1>>m1>>s;
      use[m1][t]+=s?1:-1;
    }
    cin>>q;
    //cerr<<q<<endl;

    rep(i,q){
      int ts,te,m; cin>>ts>>te>>m;
      int prev=use[m].begin()->first,in=0,ans=0;
      fr(j,use[m]){
	if(in>0)ans+=max(0,min(j->first,te)-max(prev,ts));
	prev=j->first;
	in+=j->second;
      }
      cout<<ans<<endl;
    }
  }
  return 0;
}

Measured memory_bytes: 0
#define NDEBUG

#include <unordered_map>
#include <functional>
#include <algorithm>
#include <iostream>
#include <cassert>
#include <cstring>
#include <iomanip>
#include <utility>
#include <string>
#include <vector>
#include <queue>
#include <tuple>
#include <set>
#include <map>

// [0, max)
#define FOR0(var, max) for (sl (var) = 0; (var) < (max); ++(var))
// [min, max)
#define FORi(var, min, max) for (sl (var) = (min); (var) < (max); ++(var))
// [min, max)
#define FORi_INV(var, min, max) for (sl (var) = (max) - 1; (var) + 1 > (min); --(var))
#define FORITER(var, iter) for (auto (iter) = (var).begin(); (iter) != (var).end(); (iter)++)
#define FORITER_INV(var, iter) for (auto (iter) = (var).rbegin(); (iter) != (var).rend(); (iter)++)

// a < b < c
#define LTLT(a, b, c) (((a) < (b)) && ((b) < (c)))
// a <= b < c
#define LELT(a, b, c) (((a) <= (b)) && ((b) < (c)))
// a < b <= c
#define LTLE(a, b, c) (((a) < (b)) && ((b) <= (c)))
// a <= b <= c
#define LELE(a, b, c) (((a) <= (b)) && ((b) <= (c)))

using namespace std;

using uc = unsigned char;
using ui = unsigned int;
using ul = unsigned long long int;

using sc = signed char;
using si = signed int;
using sl = signed long long int;

using ld = long double;

template <class T>
T powmod(T x, sl y, sl z) {
  T cur = x;
  T res = 1;
  while (y != 0) {
    if ((y & 1) == 1) {
      res = (res * cur) % z;
    }
    cur = (cur * cur) % z;
    y >>= 1;
  }
  return res;
}

const static sl NMAX = 100000;

static sl N;
static sl Di[NMAX];

const static sl MOD = 998244353;

void solve(void) {
  vector<sl> count;
  sl max_d = 0;
  count.resize(N, 0);
  FOR0(i, N) {
    count[Di[i]]++;
    max_d = max(max_d, Di[i]);
  }

  if (Di[0] != 0 || count[0] != 1) {
    cout << 0 << endl;
    return;
  }

  sl res = 1;

  FORi(i, 1, max_d + 1) {
    res *= powmod(count[i - 1], count[i], MOD);
    res %= MOD;
  }
  cout << res << endl;
}

int main(void) {
  cin >> N;
  FOR0(i, N) {
    cin >> Di[i];
  }
  solve();
  return 0;
}

Measured memory_bytes: 1792
#include <iostream>
#include <vector>
#include <map>
#include <set>
#include <unordered_map>
#include <unordered_set>
#include <string>
#include <cmath>
#include <limits>
#include <algorithm>

constexpr long long MOD = 1000000007;

int main()
{
  constexpr std::size_t LIMIT = 1 << 6;
  constexpr std::size_t A = 0b00;
  constexpr std::size_t T = 0b01;
  constexpr std::size_t G = 0b10;
  constexpr std::size_t C = 0b11;

  constexpr std::size_t AGC = A | (G << 2) | (C << 4);
  constexpr std::size_t GAC = G | (A << 2) | (C << 4);
  constexpr std::size_t ACG = A | (C << 2) | (G << 4);
  int N;
  std::cin >> N;
  std::vector<std::vector<long long>> dp(N + 1, std::vector<long long>(LIMIT, 0));

  for (int i = 0; i < LIMIT; ++i)
  {
    if (i != AGC &&
        i != GAC &&
        i != ACG)
    {
      dp[3][i] = 1;
    }
  }

  for (int i = 4; i <= N; ++i)
  {
    for (std::size_t j = 0; j < LIMIT; ++j)
    {
      if (j != AGC &&
          j != GAC &&
          j != ACG)
      {
        int c0 = j >> 4;
        int c1 = (j >> 2) & 0b0011;
        int c2 = j & 0b000011;
        for (std::size_t k = 0; k < (1 << 2); ++k)
        {
          auto l = ((j & 0b001111) << 2) | k;
          if (!(c0 == C && c1 == G && k == A) &&
              !(c0 == C && c2 == G && k == A))
            dp[i][j] = (dp[i][j] + dp[i - 1][l]) % MOD;
        }
      }
    }
  }

  
  long long ans = 0;
  for (std::size_t i = 0; i < LIMIT; ++i)
    ans = (ans + dp[N][i]) % MOD;
  std::cout << ans << std::endl;
  
  return 0;
}



Measured memory_bytes: 256
def cmb(n, r):
    if n - r < r: r = n - r
    if r < 0 : return 0
    if r == 0: return 1
    if r == 1: return n

    numerator = [n - r + k + 1 for k in range(r)]
    denominator = [k + 1 for k in range(r)]

    for p in range(2,r+1):
        pivot = denominator[p - 1]
        if pivot > 1:
            offset = (n - r) % p
            for k in range(p-1,r,p):
                numerator[k - offset] /= pivot
                denominator[k] /= pivot

    result = 1
    for k in range(r):
        if numerator[k] > 1:
            result *= int(numerator[k])

    return result


n,a,b=map(int,input().split())
v=list(map(float,input().split()))
v=sorted(v,reverse=True)
V=0
for i in range(a):
  V=V+v[i]
ans1=V/a
print(ans1)
ans2=0
if v[0]==ans1:
  c=v.count(ans1)
  for i in range(a,b+1):
    ans2=ans2+cmb(c,i)
  print(ans2)
else:
  d=0
  e=0
  for i in range(a):
    if v[i]==v[a]:
      d=d+1
  for i in range(n):
    if v[i]==v[a]:
      e=e+1
  ans2=cmb(e,d)
  print(ans2)

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

#define pb push_back
#define f first
#define s second
using namespace std;


int main()
{
   int a,b;
   cin>>a>>b;
   int maxb=(b+1)*10-1;
   int maxa=(a+1)*25;
   a=a*25;
   
   if(a%2)
    {a=a/2+1;
    maxa/=2;
    maxa-=1;
    }
   else {a=a/2;maxa/=2;}
   b=b*10;


   if(maxa<b||maxb<a){cout<<"-1";return 0;}
   cout<<max(a,b);

return 0;
}



Measured memory_bytes: 256
n, m = map(int, input().split())
a = list(map(int, input().split()))
a.sort(reverse=True)
suma = 0
for i in range(n):
    suma += a[i]
b = suma / (4 * m)
if a[m - 1] >= b:
    print('Yes')
else:
    print('No')

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

const int inf=1e9;
int dp[10000010][2];

int main(){
    string s; cin >> s;
    int N=s.size();
    dp[0][0]=0;
    dp[0][1]=1;
    for(int i=0;i<N;i++){
        int d=s[i]-'0';
        dp[i+1][0]=min(dp[i][0]+d,dp[i][1]+10-d);
        dp[i+1][1]=min(dp[i][0]+d+1,dp[i][1]+10-(d+1));
    }
    cout << dp[N][0] << endl;
}

Measured memory_bytes: 9476
#include<bits/stdc++.h>
using namespace std;
#define ll long long int
int main()
{
	string s;
	cin>>s;
	ll n=s.length();
	
	string u="",v="";
	ll x=1;
	for(ll i=0;i<n;i++){
		u+=s[i];
		if(x==(n-1)/2) break;
		x++;
	}
	
	for(ll i=x+1;i<n;i++){
		v+=s[i];
	}
	
	string a=s,b=u,c=v;
	reverse(a.begin(),a.end());
	reverse(b.begin(),b.end());
	reverse(c.begin(),c.end());
	
	if(s==a&&b==u&&v==c){
		cout<<"Yes"<<endl;
	}
	else{
		cout<<"No"<<endl;
	}
	
}

Measured memory_bytes: 256
#include <bits/stdc++.h>
#define rep(i, n) for(int i = 0; i < (int)n; i++)
#define rfor(i, a, b) for(int i = a; i < (int)b; i++)
#define all(ary) (ary).begin(), (ary).end()
#define debug(x) cerr << #x << ": " << x << '\n'
const long long INF = 1LL << 60;
const int MOD = 1000000007; // 1e9 + 7
using namespace std;
using vec = vector<int>;
using ll = long long;
template <class T = int> T in(){ T x; cin >> x; return (x);}
template <class T> inline bool chmin(T &a, T b) { if ( a > b ) { a = b; return 1; } return 0; }
template <class T> inline bool chmax(T &a, T b) { if ( a < b ) { a = b; return 1; } return 0; }
const int dy[4] = {-1, 0, 0, 1};
const int dx[4] = {0, -1, 1, 0};

int N, M;
ll res = 0;

int main(int argc, char *argv[]){
    cin >> N >> M;
    if(N >= M){ cout << 0 << endl; return 0; }
    vec X(M);
    rep(i, M) cin >> X[i];
    sort(all(X));
    vector<ll> L(M - 1);
    rfor(i, 0, M - 1) L[i] = X[i + 1] - X[i];
    sort(all(L), greater<ll>());
    res = X.back() - X[0];
    rep(i, min(M - 1, N - 1)) res -= L[i];
    cout << res << endl;
    return 0;
}

Measured memory_bytes: 1408
#include<iostream>

using namespace std;
int m, n;
int fld[90][90];
const pair<int, int> d[]={{1, 0}, {-1, 0}, {0, 1}, {0, -1}};

int solve(int i, int j, int num){ // broken ice
    int ret=num;
    for(int k=0; k<4; k++){
        pair<int, int> nx=make_pair(i+d[k].first, j+d[k].second);
        if(0<=nx.first&&nx.first<n&&0<=nx.second&&nx.second<m){
            if(fld[nx.first][nx.second]==1){
                fld[nx.first][nx.second]=0;
                ret=max(ret, solve(nx.first, nx.second, num+1));
                fld[nx.first][nx.second]=1;
            }
        }
    }
    return ret;
}

int main(){

    while(1){
        cin>> m>> n;// width, hight
        if(m==0&&n==0) break;
        for(int i=0; i<n; i++){
            for(int j=0; j<m; j++){
                cin>> fld[i][j];
            }
        }

        int ans=0;
        for(int i=0; i<n; i++){
            for(int j=0; j<m; j++){
                if(fld[i][j]==1){
                    fld[i][j]=0;
                    ans=max(ans, solve(i, j, 1));
                    fld[i][j]=1;
                }
            }
        }

        cout<< ans<< endl;

    }

    return 0;
}

Measured memory_bytes: 3016
#include <cstdio>
#include <cstdint>
#include <cinttypes>
#include <iostream>
#include <vector>
#include <utility>
#include <cmath>
#include <algorithm>
#include <numeric>
#include <string>
using namespace std;
using u8t = std::uint64_t;
using i8t = std::int64_t;
#define F(I,N) for(i8t I=0,I##_N=N;I<I##_N;I++)
#define FR(I,N) for(i8t I=N;I--;)
#define R(V) i8t V;std::cin>>V;
#define RV(V,N) std::vector<i8t> V;F(_i,N){R(_x)V.push_back(_x);}
#define RL1(line) std::getline(std::cin,line);
#define RL(line) std::string line;RL1(line)
#define RM(V,L,C) std::vector<char> V;F(_i,L){RL(_l)F(_j,C)V.push_back(_l[_j]);}
#define P(X) std::cout<<(X)<<std::endl;

struct infant{
  i8t opos;
  i8t active;
};

int main() {
  R(N)RV(A,N);
  std::vector<infant> arr;
  F(I,N){
    infant x;
    x.opos=I+1;
    x.active=A[I];
    arr.push_back(x);
  }
  std::sort(arr.begin(),arr.end(),[](auto const& a, auto const& b){return a.active > b.active;});

  i8t score=0;
  i8t left = 1, right = N;

  // scores[j] = (左端がj人埋まっている場合の最高得点)
  std::vector<i8t> scores;
  scores.push_back(0);
  std::vector<i8t> scores2;

  for (int i=0;i<N;i++){
    auto const& inf1 = arr[i];
    scores2.clear();
    scores2.push_back(0);
    for(int j=0;j<scores.size();j++){
      i8t l=1+j;
      i8t r=l+(N-i-1);
      auto const mvl = inf1.active*(inf1.opos - l);
      auto const mvr = inf1.active*(r - inf1.opos);
      scores2[j]=std::max(scores2[j],scores[j]+mvr);
      scores2.push_back(scores[j]+mvl);
    }
    scores.swap(scores2);
  }
  auto it = max_element(scores.begin(), scores.end());
  P(*it);
  return 0;
}


Measured memory_bytes: 3708
import java.util.*;

public class Main {
  public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    int number, i, sum = 0, tmp = -1;
    number = Integer.parseInt(sc.next());
    int[] order = new int[number];
    int[] score = new int[number];
    int[] additional = new int[number-1];
    for(i = 0; i < number; i++) {
      order[i] = Integer.parseInt(sc.next());
    }
    for(i = 0; i < number; i++) {
      score[i] = Integer.parseInt(sc.next());
    }
    for(i = 0; i < number-1; i++) {
      additional[i] = Integer.parseInt(sc.next());
    }
    for(i = 0; i < number; i++) {
      sum += score[order[i] - 1];
      if(tmp + 1 == order[i]) sum += additional[tmp - 1];
      tmp = order[i];
    }
    System.out.println(sum);
  }
}


Measured memory_bytes: 23764
###==================================================
### import
#import bisect
#from collections import Counter, deque, defaultdict
#from copy import deepcopy
#from functools import reduce, lru_cache
#from heapq import heappush, heappop
#import itertools
#import math
#import string
import sys
### stdin
def input(): return sys.stdin.readline().rstrip()
def iIn(): return int(input())
def iInM(): return map(int, input().split())
def iInM1(): return map(int1, input().split())
def iInLH(): return list(map(int, input().split()))
def iInLH1(): return list(map(int1, input().split()))
def iInLV(n): return [iIn() for _ in range(n)]
def iInLV1(n): return [iIn()-1 for _ in range(n)]
def iInLD(n): return [iInLH() for _ in range(n)]
def iInLD1(n): return [iInLH1() for _ in range(n)]
def sInLH(): return list(input().split())
def sInLV(n): return [input().rstrip('\n') for _ in range(n)]
def sInLD(n): return [sInLH() for _ in range(n)]
### stdout
def OutH(lst, deli=' '): print(deli.join(map(str, lst)))
def OutV(lst): print('\n'.join(map(str, lst)))
### setting
sys.setrecursionlimit(10 ** 6)
### utils
int1 = lambda x: int(x) - 1
### constants
INF = int(1e9)
MOD = 1000000007
dx = (-1, 0, 1, 0)
dy = (0, -1, 0, 1)
###---------------------------------------------------

N, M = iInM()
KS = iInLD(M)
P = iInLH()

ans = 0
for i in range(2**N):
    isOn = [False] * N
    for j in range(N):
        isOn[N-1-j] = ((i >> j) & 1) == 1
    for j in range(M):
        cnt = 0
        for k in range(KS[j][0]):
            cnt += isOn[KS[j][k+1]-1]
        if cnt % 2 != P[j]:
            break
    else:
        ans += 1
    #print(isOn, cnt)

print(ans)


Measured memory_bytes: 74064
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time,copy,functools

sys.setrecursionlimit(10**7)
inf = 10**20
eps = 1.0 / 10**13
mod = 10**9+7
dd = [(-1,0),(0,1),(1,0),(0,-1)]
ddn = [(-1,0),(-1,1),(0,1),(1,1),(1,0),(1,-1),(0,-1),(-1,-1)]

def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def S(): return input()
def pf(s): return print(s, flush=True)

def distance(x1, y1, x2, y2):
    return math.sqrt((x1-x2)**2 + (y1-y2)**2)

def radian(x1, y1, x2, y2):
    return math.atan2(y2-y1, x2-x1)

# (distance, radian)
def drx(d, r):
    return math.cos(r) * d

def dry(d, r):
    return math.sin(r) * d

def main():
    x1,y1,x2,y2 = LI()
    d = distance(x1, y1, x2, y2)
    r = radian(x1, y1, x2, y2)
    x3 = x1 + int(round(drx(d*(2**0.5), r+math.pi/4)))
    y3 = y1 + int(round(dry(d*(2**0.5), r+math.pi/4)))
    x4 = x1 + int(round(drx(d, r+math.pi/2)))
    y4 = y1 + int(round(dry(d, r+math.pi/2)))

    return '{} {} {} {}'.format(x3,y3,x4,y4)


print(main())


Measured memory_bytes: 7564
#include<bits/stdc++.h>
using namespace std;
#define ll long long int
#define INF 1e18
int MOD = 1e9+7;
ll a[403];
ll n,K;
ll dp[403][403];
//N
int main(){
	cin>>n;
	for(int i=0;i<n;++i){ //initialize dp
		for(int j=0;j<n;++j){
			dp[i][j] = (ll)INF;
		}
	}
	vector<ll> pref(n+1);
	for(int i=0;i<n;++i){
		cin>>a[i];
		pref[i+1] = pref[i]+a[i];
	}
	for(int L=1;L<=n;L++){
		for(int i=0;i<=n-L;i++){
			int j = i+L-1;
			if(L==1) {dp[i][j] = 0;continue;}
			ll sum = pref[j+1]-pref[i];
			for(int k=i;k<j;k++){
				dp[i][j] = min(dp[i][j],dp[i][k]+dp[k+1][j]+sum);
			}
		}
	}
	// cout<<endl;cout<<endl;
	// for(int i=0;i<n;++i){ //initialize dp
	// 	for(int j=0;j<n;++j){
	// 		cout<<dp[i][j]<<"           "; 
	// 	}
	// 	cout<<endl;
	// }
	cout<<dp[0][n-1]<<endl;
}

Measured memory_bytes: 4840
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define repr(i, n) for (int i = (n); i >= 0; --i)
#define FOR(i, m, n) for (int i = (m); i < (n); ++i)
#define FORR(i, m, n) for (int i = (m); i >= (n); --i)
#define equals(a, b) (fabs((a) - (b)) < EPS)
using namespace std;
typedef long long ll;
const ll mod = 1000000007;
const ll mod2 = 998244353;
const ll INF = 1e18;
const long double EPS = 1e-10;

int main() {
  int n, k;
  cin >> n >> k;
  
  ll ans = 0;
  FOR(b, k + 1, n + 1) {
    int a = max(0, k - 1);
    while (1) {
      if (a + b < n) {
        ans += b - k;
        a += b;
      }
      else {
        ans += min(n - a, b - k);
        break;
      }
    }
  }
  cout << ans << endl;


  
  return 0;
}

Measured memory_bytes: 256
import Control.Monad
import Control.Applicative
import Data.Array

eps :: Double
eps = 1.0e-8

type Point = (Double, Double)
add (x1, y1) (x2, y2) = (x1 + x2, y1 + y2)
sub (x1, y1) (x2, y2) = (x1 - x2, y1 - y2)
mul (x, y) s = (x * s, y * s)
nrm  (x, y) = (x / n, y / n) where n = sqrt $ (x * x + y * y)
dst  (x, y) = sqrt $ (x * x + y * y)
dst2 (x, y) = x * x + y * y
getX (x, _) = x
getY (_, y) = y

parsePoint :: String -> Point
parsePoint str =
    let [x, y] = map read $ words str
    in (x, y)

calcCenter :: Point -> Point -> (Point, Point)
calcCenter a b =
    let m = (a `add` b) `mul` 0.5
        d = sqrt (1.0 - ((dst $ a `sub` m) ** 2.0))
        e = a `sub` b
        n = nrm $ (negate $ getY e, getX e)
        c1 = m `add` (n `mul` d)
        c2 = m `sub` (n `mul` d)
    in (c1, c2)

solve' :: [Point] -> Int
solve' pts =
    let np    = length pts
        parr  = listArray (0, np-1) pts
        pairs = [ (parr ! i, parr ! j) | i <- [0..np-1], j <- [i+1..np-1] ]
    in if null pairs
        then 1
        else maximum $ map countUp pairs where
            countUp (p, q) =
                if (dst2 $ p `sub` q) >= 4.0 - eps
                    then 1
                    else
                        let (c1, c2) = calcCenter p q
                            m1 = length $ filter (\p -> (dst2 $ p `sub` c1) <= 1.0 + eps) pts
                            m2 = length $ filter (\p -> (dst2 $ p `sub` c2) <= 1.0 + eps) pts
                        in (max m1 m2)

solve :: [String] -> [Int]
solve []   = []
solve cont =
    let n           = read (head cont) :: Int
        (crds, rst) = splitAt n (tail cont)
        pts         = map parsePoint crds
    in if n == 0 then []
                 else (solve' pts) : solve rst

main :: IO ()
main = do
    contents <- getContents
    mapM_ print $ solve $ lines contents

Measured memory_bytes: 27108
n = int(input())
p = list(map(int, input().split()))
q = list(map(int, input().split()))
def fact(a):
    if a == 1:
        return 1
    else: return a * fact(a-1)
def orde(s):
    y = 1
    for i in range(len(s)-1):
        y += fact(len(s)-i-1) * (s[i]-1)
        for j in range(i, len(s)):
            if s[j] > s[i]:
                s[j] -= 1
    return y
print(abs(orde(p)-orde(q)))

Measured memory_bytes: 3064
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <cmath>
#include <stack>
#include <queue>
#include <numeric>
#include <iomanip>
#include <map>
#include <set>
#include <deque>
#include <utility>
using namespace std;
using ll = long long;
#define rep(i, n) for (int i = 0; i < n; i++)
#define repk(i, k, n) for (int i = k; i < n; i++)
#define MOD 1e9 + 7
#define INF 1e9
#define SENTINEL 2e9
#define PIE 3.14159265358979323

template <class T>
inline bool chmin(T &a, T b){if (a > b){a = b;return true;}return false;}
template <class T>
inline bool chmax(T &a, T b){if (a < b){a = b;return true;}return false;}
template <class T>
inline T GCD(T a, T b){if (b == 0) return a; else return GCD(b, a % b);}
template <class T>
inline T LCM(T a, T b) {return (a * b) / GCD(a, b);}

int main(){
    int n, m;
    cin >> n >> m;
    int l, r;
    int left = 0;
    int right = n;
    rep(i,m){
        cin >> l >> r;
        if (left < l)
            left = l;
        if (right > r)
            right = r;
    }
    if (right >= left)
        cout << right - left + 1 << endl;
    else
        cout << 0 << endl;
}

Measured memory_bytes: 256