#include <bits/stdc++.h>

typedef long long ll;
using namespace std;

#define _overload(_1, _2, _3, name, ...) name
#define _rep(i, n) for(ll i = 0; i < (ll)n; i++)
#define _repi(i, s, n) for(ll i = (ll)(s); i < (ll)n; i++)
#define REP(...) _overload(__VA_ARGS__, _repi, _rep,)(__VA_ARGS__)
#define all(v) (v).begin(),(v).end()
#define UNIQUE(v) (v).erase( unique( all(v) ), (v).end() );
#define MOD 1000000007

int main(){
	ll n, m;	cin >> n >> m;
	vector<pair<ll ,ll>> f(m*2);
	REP(i, m){
		cin >> f[i].first >> f[i].second;
		f[i].first--;
		f[i].second--;
	}
	vector<ll> group[n];
	ll ans = 0;

	REP(i, m){
		f[i+m].first = f[i].second;
		f[i+m].second = f[i].first;
	}
	sort( all(f) );
	UNIQUE( f );

	REP(i, n)	group[i].push_back(i);

	REP(i, f.size()){
		group[ f[i].first ].push_back( f[i].second );
	}

	REP(i, n){
		ans = max(ans, (ll)group[i].size());
	}

	cout << ans << endl;

	return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll ;
int main()
{
    int n, t, x, i, j ;
    cin >> n >> x >> t;
    i=(n/x)*t;
    if(n%x!=0)
        i+=t;
    cout << i ;
    return 0;
}

Global OOD problem IDs are disjoint from train, validation, and in-distribution test problem IDs.

Score : 100 points

Problem Statement

Takahashi loves takoyaki - a ball-shaped snack.

With a takoyaki machine, he can make at most X pieces of takoyaki at a time, taking T minutes regardless of the number of pieces to make.

How long does it take to make N takoyaki?

Constraints

1 \leq N,X,T \leq 1000

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N X T

Output

Print an integer representing the minimum number of minutes needed to make N pieces of takoyaki.

Sample Input 1

20 12 6

Sample Output 1

12

He can make 12 pieces of takoyaki in the first 6 minutes and 8 more in the next 6 minutes, so he can make 20 in a total of 12 minutes.

Note that being able to make 12 in 6 minutes does not mean he can make 2 in 1 minute.

Sample Input 2

1000 1 1000

Sample Output 2

1000000

It seems to take a long time to make this kind of takoyaki.

Submitted source-code file size, in bytes.

Execution CPU time reported by official Project CodeNet metadata, in milliseconds.

Peak memory usage reported by official Project CodeNet metadata, in kilobytes.


#include <iostream>
#include <cstdio>
using namespace std;
int n, x, t;
long long sum;
int main() {
	scanf("%d %d %d", &n, &x, &t);
	if(n % x != 0) {
		printf("%lld", (n / x + 1)* t);
	}
	else{
		printf("%lld", (n / x)* t);
	}
	return 0;
}


#include <bits/stdc++.h>
#define rep(i,n) for (int i = 0; i < (n); ++i)
#define size_of_array(array) (sizeof(array)/sizeof(array[0]))
#define MAX 100005
#define NIL -1
using ll =long long;
using namespace std;
using Graph=vector<vector<int>>;
using Field=vector<vector<char>>;
using P =pair<int,int>;

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 ll mod=1000000007;

int main(){
    int n,x,t;
    cin>>n>>x>>t;
    if(n%x==0){
        cout<<(n/x)*t<<endl;
    }else{
        cout<<((n/x)+1)*t<<endl;
    }

}


#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

int main(void){
	int N, fumi=0;
	cin >> N;
	ll A[N];
	for(int i=0; i<N; i++){
      cin >> A[i];
    }
  	ll fr, cur, dif;
  	for(int i=1; i<N; i++){
      fr = A[i-1]; cur = A[i];
      dif = (fr > cur)*(fr-cur);
      A[i] += dif;
      fumi += dif;
    }

  	cout << fumi << endl;

	return 0;
}

Score : 300 points

Problem Statement

N persons are standing in a row. The height of the i-th person from the front is A_i.

We want to have each person stand on a stool of some heights - at least zero - so that the following condition is satisfied for every person:

Condition: Nobody in front of the person is taller than the person. Here, the height of a person includes the stool.

Find the minimum total height of the stools needed to meet this goal.

Constraints

1 \leq N \leq 2\times 10^5

1 \leq A_i \leq 10^9

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
A_1 \ldots A_N

Output

Print the minimum total height of the stools needed to meet the goal.

Sample Input 1

5
2 1 5 4 3

Sample Output 1

4

If the persons stand on stools of heights 0, 1, 0, 1, and 2, respectively, their heights will be 2, 2, 5, 5, and 5, satisfying the condition.

We cannot meet the goal with a smaller total height of the stools.

Sample Input 2

5
3 3 3 3 3

Sample Output 2

0

Giving a stool of height 0 to everyone will work.


#include <cstdio>
char s[5];int ans=-1,num;
int main(){
    scanf("%s",s);
    for(int i=0;s[i];i++){
        if(s[i]=='R'&&(s[i]==s[i-1]||i==0)) ++num;
        if(ans<num) ans=num;
    }
    printf("%d\n",ans);
    return 0;
}

Score : 100 points

Problem Statement

We have weather records at AtCoder Town for some consecutive three days. A string of length 3, S, represents the records - if the i-th character is S, it means it was sunny on the i-th day; if that character is R, it means it was rainy on that day.

Find the maximum number of consecutive rainy days in this period.

Constraints

|S| = 3

Each character of S is S or R.

Input

Input is given from Standard Input in the following format:

S

Output

Print the maximum number of consecutive rainy days in the period.

Sample Input 1

RRS

Sample Output 1

2

We had rain on the 1-st and 2-nd days in the period. Here, the maximum number of consecutive rainy days is 2, so we should print 2.

Sample Input 2

SSS

Sample Output 2

0

It was sunny throughout the period. We had no rainy days, so we should print 0.

Sample Input 3

RSR

Sample Output 3

1

We had rain on the 1-st and 3-rd days - two "streaks" of one rainy day, so we should print 1.


#include <bits/stdc++.h>
using namespace std;
#define REP(i,n) for(int i=0;i<(int)(n);i++)
typedef long long ll;

int main() {
	ll Ans=0;
	ll X; cin >> X;
	ll K; cin >> K;
	ll D; cin >> D;

	ll Now=X;
	REP(i,K){
		if (Now>=0){
			Now-=D;
		}else{
			Now+=D;
		}
	}
	cout << abs(Now) << endl;
	return 0;
}

Score : 300 points

Problem Statement

Takahashi, who lives on the number line, is now at coordinate X. He will make exactly K moves of distance D in the positive or negative direction.

More specifically, in one move, he can go from coordinate x to x + D or x - D.

He wants to make K moves so that the absolute value of the coordinate of the destination will be the smallest possible.

Find the minimum possible absolute value of the coordinate of the destination.

Constraints

-10^{15} \leq X \leq 10^{15}

1 \leq K \leq 10^{15}

1 \leq D \leq 10^{15}

All values in input are integers.

Input

Input is given from Standard Input in the following format:

X K D

Output

Print the minimum possible absolute value of the coordinate of the destination.

Sample Input 1

6 2 4

Sample Output 1

2

Takahashi is now at coordinate 6. It is optimal to make the following moves:

Move from coordinate 6 to (6 - 4 =) 2.

Move from coordinate 2 to (2 - 4 =) -2.

Here, the absolute value of the coordinate of the destination is 2, and we cannot make it smaller.

Sample Input 2

7 4 3

Sample Output 2

1

Takahashi is now at coordinate 7. It is optimal to make, for example, the following moves:

Move from coordinate 7 to 4.

Move from coordinate 4 to 7.

Move from coordinate 7 to 4.

Move from coordinate 4 to 1.

Here, the absolute value of the coordinate of the destination is 1, and we cannot make it smaller.

Sample Input 3

10 1 2

Sample Output 3

8

Sample Input 4

1000000000000000 1000000000000000 1000000000000000

Sample Output 4

1000000000000000

The answer can be enormous.


#include <cassert>
#include <cctype>
#include <cerrno>
#include <cfloat>
#include <ciso646>
#include <climits>
#include <clocale>
#include <cmath>
#include <csetjmp>
#include <csignal>
#include <cstdarg>
#include <cstddef>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>

#include <ccomplex>
#include <cfenv>
#include <cinttypes>
#include <cstdbool>
#include <cstdint>
#include <ctgmath>
#include <cwchar>
#include <cwctype>

// C++
#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <exception>
#include <fstream>
#include <functional>
#include <iomanip>
#include <ios>
#include <iosfwd>
#include <iostream>
#include <istream>
#include <iterator>
#include <limits>
#include <list>
#include <locale>
#include <map>
#include <memory>
#include <new>
#include <numeric>
#include <ostream>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdexcept>
#include <streambuf>
#include <string>
#include <typeinfo>
#include <utility>
#include <valarray>
#include <vector>

#include <array>
#include <atomic>
#include <chrono>
#include <condition_variable>
#include <forward_list>
#include <future>
#include <initializer_list>
#include <mutex>
#include <random>
#include <ratio>
#include <regex>
#include <scoped_allocator>
#include <system_error>
#include <thread>
#include <tuple>
#include <typeindex>
#include <type_traits>
#include <unordered_map>
#include <unordered_set>
using namespace std;
#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define endl "\n"
#define f(i, n) for(int i = 0; i < n; ++i)
#define pb push_back
#define eb emplace_back
#define m_p make_pair
#define T int t = 0; cin >> t; while(t--)
#define pr(a) for(int i = 0; i < (int)a.size(); ++i)cout << a[i] << " ";cout << endl;
#define b_e(a) a.begin(), a.end()
typedef long long ll;
typedef long double lld;
const ll mod = 1e15;
const ll mx = 1e3 + 1;
const ll inf = (1LL << 62LL);
const int sz = 256;
const int max_k = 20;
typedef long long ll;

void input(){
    IOS;
    ll n, k;
    cin >> n >> k;
    vector<ll>p(n), c(n);
    f(i, n)cin >> p[i], --p[i];
    f(i, n)cin >> c[i];
    ll ans = -inf;
    f(i, n){
        ll start = p[i];
        vector<ll>temp;
        temp.push_back(c[start]);
        while(i != start){
            start = p[start];
            temp.push_back(c[start]);
        }
        ll t = (int)temp.size();
        vector<ll>pre(t);
        pre[0] = temp[0];
        for(int i = 1; i < t; ++i)pre[i] += pre[i - 1] + temp[i];
        for(ll r : {0LL, 1LL, k / t, k / t - 1, k / t - 2, 2LL}){
            if(t * r > k || r < 0)continue;
            if(t * r == k){
                ans = max(ans, pre[t - 1] * r);
            }else{
                ll y = k - r * t;
                ll as = -inf;
                f(j, min(t, y))as = max(as, pre[j]);
                ans = max(ans, pre[t - 1] * r + as);
            }
        }
    }
    cout << ans << endl;

}

int main(){
    input();
    return 0;
}
/*
freopen("input.txt","r",stdin);
freopen("output.txt","w",stdout);
*/

Score : 400 points

Problem Statement

Takahashi will play a game using a piece on an array of squares numbered 1, 2, \cdots, N. Square i has an integer C_i written on it. Also, he is given a permutation of 1, 2, \cdots, N: P_1, P_2, \cdots, P_N.

Now, he will choose one square and place the piece on that square. Then, he will make the following move some number of times between 1 and K (inclusive):

In one move, if the piece is now on Square i (1 \leq i \leq N), move it to Square P_i. Here, his score increases by C_{P_i}.

Help him by finding the maximum possible score at the end of the game. (The score is 0 at the beginning of the game.)

Constraints

2 \leq N \leq 5000

1 \leq K \leq 10^9

1 \leq P_i \leq N

P_i \neq i

P_1, P_2, \cdots, P_N are all different.

-10^9 \leq C_i \leq 10^9

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N K
P_1 P_2 \cdots P_N
C_1 C_2 \cdots C_N

Output

Print the maximum possible score at the end of the game.

Sample Input 1

5 2
2 4 5 1 3
3 4 -10 -8 8

Sample Output 1

8

When we start at some square of our choice and make at most two moves, we have the following options:

If we start at Square 1, making one move sends the piece to Square 2, after which the score is 4. Making another move sends the piece to Square 4, after which the score is 4 + (-8) = -4.

If we start at Square 2, making one move sends the piece to Square 4, after which the score is -8. Making another move sends the piece to Square 1, after which the score is -8 + 3 = -5.

If we start at Square 3, making one move sends the piece to Square 5, after which the score is 8. Making another move sends the piece to Square 3, after which the score is 8 + (-10) = -2.

If we start at Square 4, making one move sends the piece to Square 1, after which the score is 3. Making another move sends the piece to Square 2, after which the score is 3 + 4 = 7.

If we start at Square 5, making one move sends the piece to Square 3, after which the score is -10. Making another move sends the piece to Square 5, after which the score is -10 + 8 = -2.

The maximum score achieved is 8.

Sample Input 2

2 3
2 1
10 -7

Sample Output 2

13

Sample Input 3

3 3
3 1 2
-1000 -2000 -3000

Sample Output 3

-1000

We have to make at least one move.

Sample Input 4

10 58
9 1 6 7 8 4 3 2 10 5
695279662 988782657 -119067776 382975538 -151885171 -177220596 -169777795 37619092 389386780 980092719

Sample Output 4

29507023469

The absolute value of the answer may be enormous.


#ifdef LOCAL
    #define _GLIBCXX_DEBUG
    #define __clock__
#else
    #pragma GCC optimize("Ofast")
#endif
#include<bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using VI = vector<ll>;
using VV = vector<VI>;
using VS = vector<string>;
using PII = pair<ll, ll>;

// tourist set
template <typename A, typename B>
string to_string(pair<A, B> p);

template <typename A, typename B, typename C>
string to_string(tuple<A, B, C> p);

template <typename A, typename B, typename C, typename D>
string to_string(tuple<A, B, C, D> p);

string to_string(const string& s) {
  return '"' + s + '"';
}

string to_string(const char* s) {
  return to_string((string) s);
}

string to_string(bool b) {
  return (b ? "true" : "false");
}

string to_string(vector<bool> v) {
  bool first = true;
  string res = "{";
  for (int i = 0; i < static_cast<int>(v.size()); i++) {
    if (!first) {
      res += ", ";
    }
    first = false;
    res += to_string(v[i]);
  }
  res += "}";
  return res;
}

template <size_t N>
string to_string(bitset<N> v) {
  string res = "";
  for (size_t i = 0; i < N; i++) {
    res += static_cast<char>('0' + v[i]);
  }
  return res;
}

template <typename A>
string to_string(A v) {
  bool first = true;
  string res = "{";
  for (const auto &x : v) {
    if (!first) {
      res += ", ";
    }
    first = false;
    res += to_string(x);
  }
  res += "}";
  return res;
}

template <typename A, typename B>
string to_string(pair<A, B> p) {
  return "(" + to_string(p.first) + ", " + to_string(p.second) + ")";
}

template <typename A, typename B, typename C>
string to_string(tuple<A, B, C> p) {
  return "(" + to_string(get<0>(p)) + ", " + to_string(get<1>(p)) + ", " + to_string(get<2>(p)) + ")";
}

template <typename A, typename B, typename C, typename D>
string to_string(tuple<A, B, C, D> p) {
  return "(" + to_string(get<0>(p)) + ", " + to_string(get<1>(p)) + ", " + to_string(get<2>(p)) + ", " + to_string(get<3>(p)) + ")";
}

void debug_out() { cerr << '\n'; }

template <typename Head, typename... Tail>
void debug_out(Head H, Tail... T) {
  cerr << " " << to_string(H);
  debug_out(T...);
}

#ifdef LOCAL
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
#else
#define debug(...) 42
#endif
// tourist set end

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

#define FOR(i,a,b) for(ll i=(a);i<(b);++i)
#define rep(i,b) FOR(i, 0, b)
#define ALL(v) (v).begin(), (v).end()
#define p(s) cout<<(s)<<'\n'
#define p2(s, t) cout << (s) << " " << (t) << '\n'
#define br() p("")
#define pn(s) cout << (#s) << " " << (s) << '\n'
#define SZ(x) ((int)(x).size())
#define SORT(A) sort(ALL(A))
#define RSORT(A) sort(ALL(A), greater<ll>())
#define MP make_pair
#define p_yes() p("YES")
#define p_no() p("NO")

ll SUM(VI& V){
  return accumulate(ALL(V), 0LL);
}

ll MIN(VI& V){return *min_element(ALL(V));}
ll MAX(VI& V){return *max_element(ALL(V));}

void print_vector(VI& V){
  ll n = V.size();
  rep(i, n){
    if(i) cout << ' ';
    cout << V[i];
  }
  cout << endl;
}

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

ll lcm(ll a,ll b){
    ll g = gcd(a,b);
    return a / g * b;
}

// long double
using ld = long double;
#define EPS (1e-14)
#define equals(a,b) (fabs((a)-(b)) < EPS)

void no(){p_no(); exit(0);}
void yes(){p_yes(); exit(0);}

const ll mod = 1e9 + 7;
const ll inf = 1e18;
const double PI = acos(-1);

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

  // for prime mod
  mint inv() const {
    return pow(mod-2);
  }
  mint& operator/=(const mint a) {
    return (*this) *= a.inv();
  }
  mint operator/(const mint a) const {
    mint res(*this);
    return res/=a;
  }
};

template< unsigned mod >
struct RollingHash {
  vector< unsigned > hashed, power;

  inline unsigned mul(unsigned a, unsigned b) const {
    unsigned long long x = (unsigned long long) a * b;
    unsigned xh = (unsigned) (x >> 32), xl = (unsigned) x, d, m;
    asm("divl %4; \n\t" : "=a" (d), "=d" (m) : "d" (xh), "a" (xl), "r" (mod));
    return m;
  }

  // RollingHash(){

  // }

  RollingHash(const string &s, unsigned base = 10007) {
    int sz = (int) s.size();
    hashed.assign(sz + 1, 0);
    power.assign(sz + 1, 0);
    power[0] = 1;
    for(int i = 0; i < sz; i++) {
      power[i + 1] = mul(power[i], base);
      hashed[i + 1] = mul(hashed[i], base) + s[i];
      if(hashed[i + 1] >= mod) hashed[i + 1] -= mod;
    }
  }

  unsigned get(int l, int r) const {
    unsigned ret = hashed[r] + mod - mul(hashed[l], power[r - l]);
    if(ret >= mod) ret -= mod;
    return ret;
  }

  unsigned connect(unsigned h1, int h2, int h2len) const {
    unsigned ret = mul(h1, power[h2len]) + h2;
    if(ret >= mod) ret -= mod;
    return ret;
  }

  int LCP(const RollingHash< mod > &b, int l1, int r1, int l2, int r2) {
    int len = min(r1 - l1, r2 - l2);
    int low = -1, high = len + 1;
    while(high - low > 1) {
      int mid = (low + high) / 2;
      if(get(l1, l1 + mid) == b.get(l2, l2 + mid)) low = mid;
      else high = mid;
    }
    return (low);
  }
};

using RH = RollingHash< 1000000007 >;

// できること
// 一点追加
// 範囲和
// ei1333's BIT
struct BIT {
  VI data;

  BIT(ll sz) {
    data.assign(++sz, 0);
  }

  ll sum(ll k) {
    ll ret = 0;
    for(++k; k > 0; k -= k & -k) ret += data[k];
    return (ret);
  }

  // [i, j]
  ll range_sum(ll i, ll j){
    if(i==0){
      return sum(j);
    }else{
      return sum(j) - sum(i-1);
    }
  }

  void add(ll k, ll x) {
    for(++k; k < data.size(); k += k & -k) data[k] += x;
  }
};

struct Hoge{
  vector<BIT> bits;

  Hoge(string& s){
    ll N = s.size();
    rep(i,26){
      auto bi = BIT(N);
      bits.push_back(bi);
    }
    rep(i,N){
      ll v = s[i]-'a';
      bits[v].add(i, 1);
    }
  }

  ll range_count(ll l, ll r, char c){
    ll v = c-'a';
    return bits[v].range_sum(l,r);
  }
};

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

    // input
    ll N;
    cin>>N;

    if(N>500){
      assert(1==2);
    }

    VS S(N);
    rep(i,N)cin>>S[i];

    // sからtは作れるか
    auto f = [&](string s, string t){
      if(t.size()==1){
        char ch = t[0];
        for(char c : s){
          if(c==ch) return true;
        }
        return false;
      }

      if(SZ(s)<=SZ(t)) return false;

      ll sL = SZ(s);
      ll tL = SZ(t);

      // |t|>1
      // しっぽは同じ
      string tail = t.substr(1);
      ll tailL = SZ(tail);
      if(s.substr(sL-tailL) != tail) return false;
      char head = t[0];
      rep(i,sL-tailL){
        if(s[i]==head) return true;
      }
      return false;
    };

    ll cnt=0;
    rep(i,N){
      rep(j,N){
        if(i==j) continue;
        if(f(S[i],S[j])){
          debug(S[i],S[j]);
          cnt++;
        }
      }
    }
    p(cnt);

    return 0;
}

Score : 700 points

Problem Statement

Limak can repeatedly remove one of the first two characters of a string,
for example abcxyx \rightarrow acxyx \rightarrow cxyx \rightarrow cyx.

You are given N different strings S_1, S_2, \ldots, S_N.
Among N \cdot (N-1) / 2 pairs (S_i, S_j),
in how many pairs could Limak obtain one string from the other?

Constraints

2 \leq N \leq 200\,000

S_i consists of lowercase English letters a-z.

S_i \neq S_j

1 \leq |S_i|

|S_1| + |S_2| + \ldots + |S_N| \leq 10^6

Input

Input is given from Standard Input in the following format.

N
S_1
S_2
\vdots
S_N

Output

Print the number of unordered pairs (S_i, S_j)
where i \neq j and Limak can obtain one string from the other.

Sample Input 1

3
abcxyx
cyx
abc

Sample Output 1

1

The only good pair is (abcxyx, cyx).

Sample Input 2

6
b
a
abc
c
d
ab

Sample Output 2

5

There are five good pairs: (b, abc), (a, abc), (abc, c), (b, ab), (a, ab).


#include <bits/stdc++.h>
using namespace std;
using lint = long long;
using pint = pair<int, int>;
using plint = pair<lint, lint>;
struct fast_ios { fast_ios(){ cin.tie(nullptr); ios::sync_with_stdio(false); cout << fixed << setprecision(20); }; } fast_ios_;
#define ALL(x) (x).begin(), (x).end()
#define FOR(i, begin, end) for(int i=(begin),i##_end_=(end);i<i##_end_;i++)
#define IFOR(i, begin, end) for(int i=(end)-1,i##_begin_=(begin);i>=i##_begin_;i--)
#define REP(i, n) FOR(i,0,n)
#define IREP(i, n) IFOR(i,0,n)
template <typename T> void ndarray(vector<T> &vec, int len) { vec.resize(len); }
template <typename T, typename... Args> void ndarray(vector<T> &vec, int len, Args... args) { vec.resize(len); for (auto &v : vec) ndarray(v, args...); }
template <typename V, typename T> void ndfill(V &x, const T &val) { x = val; }
template <typename V, typename T> void ndfill(vector<V> &vec, const T &val) { for (auto &v : vec) ndfill(v, val); }
template <typename T> bool chmax(T &m, const T q) { if (m < q) {m = q; return true;} else return false; }
template <typename T> bool chmin(T &m, const T q) { if (m > q) {m = q; return true;} else return false; }
template <typename T1, typename T2> pair<T1, T2> operator+(const pair<T1, T2> &l, const pair<T1, T2> &r) { return make_pair(l.first + r.first, l.second + r.second); }
template <typename T1, typename T2> pair<T1, T2> operator-(const pair<T1, T2> &l, const pair<T1, T2> &r) { return make_pair(l.first - r.first, l.second - r.second); }
template <typename T> vector<T> srtunq(vector<T> vec) { sort(vec.begin(), vec.end()), vec.erase(unique(vec.begin(), vec.end()), vec.end()); return vec; }
template <typename T> istream &operator>>(istream &is, vector<T> &vec) { for (auto &v : vec) is >> v; return is; }
template <typename... T> istream &operator>>(istream &is, tuple<T...> &tpl) { std::apply([&is](auto &&... args) { ((is >> args), ...);}, tpl); return is; }
template <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) { os << '['; for (auto v : vec) os << v << ','; os << ']'; return os; }
template <typename... T> ostream &operator<<(ostream &os, const tuple<T...> &tpl) { std::apply([&os](auto &&... args) { ((os << args << ','), ...);}, tpl); return os; }
template <typename T> ostream &operator<<(ostream &os, const deque<T> &vec) { os << "deq["; for (auto v : vec) os << v << ','; os << ']'; return os; }
template <typename T> ostream &operator<<(ostream &os, const set<T> &vec) { os << '{'; for (auto v : vec) os << v << ','; os << '}'; return os; }
template <typename T> ostream &operator<<(ostream &os, const unordered_set<T> &vec) { os << '{'; for (auto v : vec) os << v << ','; os << '}'; return os; }
template <typename T> ostream &operator<<(ostream &os, const multiset<T> &vec) { os << '{'; for (auto v : vec) os << v << ','; os << '}'; return os; }
template <typename T> ostream &operator<<(ostream &os, const unordered_multiset<T> &vec) { os << '{'; for (auto v : vec) os << v << ','; os << '}'; return os; }
template <typename T1, typename T2> ostream &operator<<(ostream &os, const pair<T1, T2> &pa) { os << '(' << pa.first << ',' << pa.second << ')'; return os; }
template <typename TK, typename TV> ostream &operator<<(ostream &os, const map<TK, TV> &mp) { os << '{'; for (auto v : mp) os << v.first << "=>" << v.second << ','; os << '}'; return os; }
template <typename TK, typename TV> ostream &operator<<(ostream &os, const unordered_map<TK, TV> &mp) { os << '{'; for (auto v : mp) os << v.first << "=>" << v.second << ','; os << '}'; return os; }
#ifdef HITONANODE_LOCAL
#define dbg(x) cerr << #x << " = " << (x) << " (L" << __LINE__ << ") " << __FILE__ << endl
#else
#define dbg(x)
#endif

// Rolling Hash (Rabin-Karp), 1dim
template<typename V>
struct rolling_hash
{
    V B;
    vector<V> hash; // hash[i] = s[0] * B^(i - 1) + ... + s[i - 1]
    vector<V> power; // power[i] = B^i
    rolling_hash() {}
    rolling_hash(const string &s, V b) : B(b)
    {
        int N = s.length();
        hash.resize(N + 1), power.resize(N + 1, 1);
        for (int i = 0; i < N; i++)
        {
            power[i + 1] = power[i] * B;
            hash[i + 1] = hash[i] * B + s[i];
        }
    }
    V get_hash(int l, int r) // s[l] * B^(r - l - 1) + ... + s[r - 1]
    {
        return hash[r] - hash[l] * power[r - l];
    }
};

using lint = long long;
using plint = pair<lint, lint>;
struct DoubleHash : public plint
{
    static plint MODs;
    DoubleHash(plint x) : plint(x) {}
    DoubleHash(lint x, lint y) : plint(x, y) {}
    DoubleHash(lint x) : DoubleHash(x, x) {}
    DoubleHash() : DoubleHash(0) {}
    static inline DoubleHash mod_subtract(plint x)
    {
        if (x.first >= MODs.first) x.first -= MODs.first;
        if (x.second >= MODs.second) x.second -= MODs.second;
        return x;
    }
    DoubleHash operator+(const DoubleHash &x) const
    {
        return mod_subtract(plint(this->first + x.first, this->second + x.second));
    }
    DoubleHash operator+(lint x) const
    {
        return mod_subtract(plint(this->first + x, this->second + x));
    }
    DoubleHash operator-(const DoubleHash &x) const
    {
        return mod_subtract(plint(this->first - x.first + MODs.first, this->second - x.second + MODs.second));
    }
    DoubleHash operator*(const DoubleHash &x) const
    {
        return make_pair(this->first * x.first % MODs.first, this->second * x.second % MODs.second);
    }
    DoubleHash operator*(lint x) const
    {
        return make_pair(this->first * x % MODs.first, this->second * x % MODs.second);
    }
};
plint DoubleHash::MODs = plint(1000000007, 998244353);
const DoubleHash b(8e7, 5e8);

int main()
{
    int N;
    cin >> N;
    vector<pair<size_t, string>> VS;
    REP(i, N)
    {
        string s;
        cin >> s;
        VS.emplace_back(s.length(), s);
    }
    sort(VS.begin(), VS.end());

    vector<set<DoubleHash>> dh(26);

    int ret = 0;
    for (auto [len, S] : VS)
    {
        rolling_hash<DoubleHash> rh(S, b);
        vector<int> vis(26);
        REP(i, S.length())
        {
            vis[S[i] - 'a'] = 1;
            auto h = rh.get_hash(i + 1, S.length());
            REP(d, 26) if (vis[d] and dh[d].count(h)) ret++;
        }
        dh[S[0] - 'a'].insert(rh.get_hash(1, S.length()));
    }
    cout << ret << '\n';
}


/**
 * Dont raise your voice, improve your argument.
 * --Desmond Tutu
 */

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

const bool ready = [](){
    ios_base::sync_with_stdio(false); cin.tie(0);
    cout << fixed << setprecision(12);
    return true;
}();

const double PI = acos(-1);
using ll= long long;
#define int ll
#define all(v) (v).begin(), (v).end()
#define fori(n) for(int i=0; i<int(n); i++)

#define cini(i) int i; cin>>i;
#define cins(s) string s; cin>>s;
#define cind(d) double d; cin>>d;
#define cinai(a,n) vi a(n); fori(n) { cin>>a[i]; }
#define cinas(s,n) vs s(n); fori(n) { cin>>s[i]; }
#define cinad(a,n) vd a(n); fori(n) { cin>>a[i]; }

using pii= pair<int, int>;
using pdd= pair<double, double>;
using vd= vector<double>;
using vb= vector<bool>;
using vi= vector<int>;
using vvi= vector<vi>;
using vs= vector<string>;

void solve() {
    cini(x);
    if(x>30)
        cout<<"Yes"<<endl;
    else
        cout<<"No"<<endl;
}

signed main() {
    solve();
}

// FIRST THINK, THEN CODE
// DO NOT JUMP BETWEEN PROBLEMS

Score : 100 points

Problem Statement

You will turn on the air conditioner if, and only if, the temperature of the room is 30 degrees Celsius or above.

The current temperature of the room is X degrees Celsius. Will you turn on the air conditioner?

Constraints

-40 \leq X \leq 40

X is an integer.

Input

Input is given from Standard Input in the following format:

X

Output

Print Yes if you will turn on the air conditioner; print No otherwise.

Sample Input 1

25

Sample Output 1

No

Sample Input 2

30

Sample Output 2

Yes


#include <stdio.h>
#include <math.h>
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <queue>
#include <map>
using namespace std;

int main(void){
    int N;
    cin >>N;
    if(N>=30){
        cout << "Yes"<<endl;
    }else{
        cout <<"No"<<endl;
    }
}


    #include<bits/stdc++.h>
    using namespace std;
    #define ll long long
    #define ld long double
    #define MOD 1000000007
    #define mod9 1000000009
    #define fast ios_base :: sync_with_stdio(0);cin.tie(NULL);cout.tie(NULL);
    #define mp make_pair
    #define pb push_back
    #define ct ll t;cin>>t;while(t--)
    #define bi begin()
    #define ei end()
    #define fi first
    #define se second
    #define foe(i,a,b) for(ll i=a;i<b;i++)
    #define rfoe(i,a,b) for(ll i=a;i>=0;i--)
    #define sz(s) s.size()
    #define mem(a,s) memset(a,s,sizeof(a))
    #define all(v) v.bi,v.ei
    #define MAX             8000000000000000064LL
    #define MIN            -8000000000000000064LL
    // ll MOD;
    ll add(ll a, ll b){return (a%MOD + b%MOD + ((MAX)/MOD)*MOD)%MOD;}
    ll sub(ll a, ll b){return (a%MOD - b%MOD + ((MAX)/MOD)*MOD)%MOD;}
    ll mul(ll a, ll b){return ((a%MOD)*(b%MOD) + ((MAX)/MOD)*MOD)%MOD;}
    bool isPrime(ll n)
    {
        // Corner case
        if (n <= 1)
            return false;

        // Check from 2 to n-1
        for (ll i = 2; i*i <= n; i++)
            if (n % i == 0)
                return false;

        return true;
    }
    long long binpow(long long a, long long b) {
        long long res = 1;
        while (b > 0) {
            if (b & 1)
                res = mul(res,a);
            a = mul(a,a);
            b >>= 1;
        }
        return res%MOD;
    }

    bool compare(const pair<ll,ll>&a,const pair<ll,ll>&b)
    {
        return (a.se>b.se);
    }
    ll lcm(ll a,ll b)
    {
        return(a*b)/__gcd(a,b);
    }
    //vector<ll>s;
    //// set<ll>s1;
    //bool prime[100009]={false};
    //    // set<ll>s1;
    //    void Sieve()
    //    {
    //        ll n=100009;
    //        prime[0]=prime[1]=true;
    //        // s[0]=0;
    //        // s[1]=1;
    //        // s.pb(1);
    //        // t.insert(1);
    //        for (int p=2; p*p<=n; p++)
    //        {
    //
    //        if(prime[p]==false)
    //        {
    //            //    s.pb(p);
    //            for(ll j=p*p;j<=n;j+=p)
    //            {
    //                prime[j]=true;
    //            }
    //        }
    //        }
    //        for(ll i=2;i<n;i++)
    //        {
    //            if(!prime[i])
    //            {
    //                s.pb(i);
    //                // s1.insert(i);
    //            }
    //        }
    //}
    //     ll eea(ll a, ll b, ll & x, ll & y) {
    //     if (a == 0) {
    //         x = 0;
    //         y = 1;
    //         return b;
    //     }
    //     ll x1, y1;
    //     ll d=eea(b % a, a, x1, y1);
    //     x = y1 - (b / a) * x1;
    //     y = x1;
    //     return d;
    // }
    // bool find_any_solution(ll a, ll b, ll c, ll &x0, ll &y0, ll &g) {
    //     g = eea(abs(a), abs(b), x0, y0);
    //     if (c % g) {
    //         return false;
    //     }

    //     x0 *= c / g;
    //     y0 *= c / g;
    //     if (a < 0) x0 = -x0;
    //     if (b < 0) y0 = -y0;
    //     return true;
    // }
    // ll modinverse(ll a,ll m)
    // {
    //     ll x, y;
    //     eea(a, m, x, y);
    //     if (__gcd(a,m) != 1) {
    //     return -1;
    //     }
    // else {
    //     x = (x % m + m) % m;
    //     cout << x << endl;
    //     return x;
    // }
    // }

    ll fact[1000007]={0};
    ll expo(ll x, ll y) {ll res=1;x=x%MOD;while(y>0){if(y&1)res=(1ll*res*x)%MOD;
    y=y>>1;x=(1ll*x*x)%MOD;} return res;}
    void facto() {fact[0]=1;fact[1]=1;for(ll i=2;i<1000007;i++)fact[i]=(fact[i-1]*i)%MOD;}
    ll ncr(ll n,ll r) {ll res=1; res=fact[n]; res=(res*(expo(fact[r],MOD-2)))%MOD; res=(res*(expo(fact[n-r],MOD-2)))%MOD; return res;}
    ll npr(ll n,ll r) {facto(); ll res=1; res=fact[n]; res=(res*(expo(fact[n-r],MOD-2)))%MOD; return res; }
    int const N=1e5+9;
    ll const INF = 2e18+5;

    void solve()
    {
        ll x;
        cin>>x;
        if(x>=30)
        {
            cout<<"Yes\n";
        }
        else
        {
            cout<<"No\n";
        }

    }

    int main()
    {
        fast
        // Sieve();
     //      freopen("std.in", "r", stdin);
 			 // freopen("std.out", "w", stdout);
 		 	// freopen("input.txt", "r", stdin);
			//    freopen("output.txt", "w", stdout);
        // ll g=1;
        // ct
        {
            solve();
        }
    }


#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<ll, ll>;
const ll INF = 100000000;
#define rep1(i, n) for (ll i=0; i<(n); i++)
#define rep2(i, k, n) for (ll i=k; i<(n); i++)
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }

int main() {
    int n, d, x, y;
    cin >> n >> d;
    int ans = 0;
    rep1(i, n){
        cin >> x >> y;
        if (x*x + y*y <= d*d) ans++;
    }
    cout << ans << endl;
}

Score : 200 points

Problem Statement

We have N points in the two-dimensional plane. The coordinates of the i-th point are (X_i,Y_i).

Among them, we are looking for the points such that the distance from the origin is at most D. How many such points are there?

We remind you that the distance between the origin and the point (p, q) can be represented as \sqrt{p^2+q^2}.

Constraints

1 \leq N \leq 2\times 10^5

0 \leq D \leq 2\times 10^5

|X_i|,|Y_i| \leq 2\times 10^5

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N D
X_1 Y_1
\vdots
X_N Y_N

Output

Print an integer representing the number of points such that the distance from the origin is at most D.

Sample Input 1

4 5
0 5
-2 4
3 4
4 -4

Sample Output 1

3

The distance between the origin and each of the given points is as follows:

\sqrt{0^2+5^2}=5

\sqrt{(-2)^2+4^2}=4.472\ldots

\sqrt{3^2+4^2}=5

\sqrt{4^2+(-4)^2}=5.656\ldots

Thus, we have three points such that the distance from the origin is at most 5.

Sample Input 2

12 3
1 1
1 1
1 1
1 1
1 2
1 3
2 1
2 2
2 3
3 1
3 2
3 3

Sample Output 2

7

Multiple points may exist at the same coordinates.

Sample Input 3

20 100000
14309 -32939
-56855 100340
151364 25430
103789 -113141
147404 -136977
-37006 -30929
188810 -49557
13419 70401
-88280 165170
-196399 137941
-176527 -61904
46659 115261
-153551 114185
98784 -6820
94111 -86268
-30401 61477
-55056 7872
5901 -163796
138819 -185986
-69848 -96669

Sample Output 3

6


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

int main()
{
    long long int  n=0, temp=0, count=-1;
    cin>>n;
    temp=7;
    if(temp==1 || temp== 7){
        cout<<"1"<<endl;
        return 0;
    }
    for(int i=1;i<=10000000;i++){
        if(temp==0){
            count=i;
            break;
        }
        temp=(temp*10)+7;
        temp=temp%n;
    }
    cout<<count<<endl;
}

Score : 300 points

Problem Statement

Takahashi loves the number 7 and multiples of K.

Where is the first occurrence of a multiple of K in the sequence 7,77,777,\ldots? (Also see Output and Sample Input/Output below.)

If the sequence contains no multiples of K, print -1 instead.

Constraints

1 \leq K \leq 10^6

K is an integer.

Input

Input is given from Standard Input in the following format:

K

Output

Print an integer representing the position of the first occurrence of a multiple of K. (For example, if the first occurrence is the fourth element of the sequence, print 4.)

Sample Input 1

101

Sample Output 1

4

None of 7, 77, and 777 is a multiple of 101, but 7777 is.

Sample Input 2

2

Sample Output 2

-1

All elements in the sequence are odd numbers; there are no multiples of 2.

Sample Input 3

999983

Sample Output 3

999982


#include<bits/stdc++.h>
#define pb push_back
#define pf push_front
#define clr(x) memset(x, 0, sizeof(x))
#define all(a) a.begin(),a.end()
#define forn(i, a,b) for(int i = int(a); i < int(b); ++i)
#define forn_r(i, b, a) for(int i = int(b); i > int(a); i--)
#define trav(a,x) for (auto& a : x)
#define printArray(array, length) for(int i = 0; i < length; i++) cout<< array[i] << (i == length - 1 ? '\n' : ' ');
#define print(x) cout << x << '\n'
#define print2(x, y) cout << x << ' ' << y << '\n'
#define debug(x) cout << #x << "=" << x << endl
#define debug2(x, y) cout << #x << "=" << x << "," << #y << "=" << y << endl
template <typename T> bool chmax(T &m, const T q) { if (m < q) {m = q; return true;} else return false; }
template <typename T> bool chmin(T &m, const T q) { if (m > q) {m = q; return true;} else return false; }

using namespace std;
typedef long long ll;
const int INF = 0x3f3f3f3f;
const ll LNF  = 0x3f3f3f3f3f3f;
inline int gcd(int a, int b) { return b ? gcd(b, a % b) : a; }
inline ll  gcd(ll  a, ll  b) { return b ? gcd(b, a % b) : a; }
inline int lcm(int a, int b) { return a * b / gcd(a, b); }

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int t =1;
    //cin>>t;
    while(t--){
    	int n;
    	cin>>n;
    	int count=0;
    	ll rem = 0;
  		//if(n==1 || n==7) {print(1);return 0;}
     	if(n%2==0 || n%5==0){print(-1); return 0 ;}

    	while (rem != 0) {

        	rem = (rem * 10 + 7) % n;
        	count++;
       }
       print(count+1);

    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define int long long int
int32_t main(){
    int k,i,ans=-1,tmp=0;
    for(i=1;i<=k+100;i++){
        tmp=tmp*10+7;
        tmp%=k;
        if(!tmp){ans=i;break;}
    }cout<<ans;
    return 0;
}


#include <bits/stdc++.h>
#include <variant>
#include <boost/range/algorithm.hpp>
#include <boost/range/numeric.hpp>
#include <boost/range/irange.hpp>
#include <boost/range/adaptor/indexed.hpp>
#define FOR(i,k,n)  for(int i = (k);i < (n);++i)
#define REP(i,n)    FOR(i,0,n)
#define ALL(x) begin(x),end(x)

using namespace std;
using namespace boost::adaptors;
using namespace std::string_literals;
using ll = int64_t;
using vecint = vector<int>;
using vecll = vector<ll>;
using boost::irange;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  int n;
  cin>>n;
  string s;
  cin>>s;
  int cntr = 0;
  for(auto&&c:s) {
    if (c == 'R') ++cntr;
  }
  int cntw = 0;
  int mn = cntr;
  for(auto&&c:s) {
    if (c == 'R') --cntr;
    else ++cntw;
    mn = min(mn, max(cntr, cntw));
  }
  cout<<mn<<endl;
  return 0;
}

Score : 400 points

Problem Statement

An altar enshrines N stones arranged in a row from left to right. The color of the i-th stone from the left (1 \leq i \leq N) is given to you as a character c_i; R stands for red and W stands for white.

You can do the following two kinds of operations any number of times in any order:

Choose two stones (not necessarily adjacent) and swap them.

Choose one stone and change its color (from red to white and vice versa).

According to a fortune-teller, a white stone placed to the immediate left of a red stone will bring a disaster. At least how many operations are needed to reach a situation without such a white stone?

Constraints

2 \leq N \leq 200000

c_i is R or W.

Input

Input is given from Standard Input in the following format:

N
c_{1}c_{2}...c_{N}

Output

Print an integer representing the minimum number of operations needed.

Sample Input 1

4
WWRR

Sample Output 1

2

For example, the two operations below will achieve the objective.

Swap the 1-st and 3-rd stones from the left, resulting in RWWR.

Change the color of the 4-th stone from the left, resulting in RWWW.

Sample Input 2

2
RR

Sample Output 2

0

It can be the case that no operation is needed.

Sample Input 3

8
WRWWRWRR

Sample Output 3

3


#include<bits/stdc++.h>
#include<cctype>
using namespace std;
#define rep(i,n) for (int i=0;i<(n);i++)
#define reps(i,n) for (int i=1;i<=(n);i++)
#define rrep(i,n) for (int i=(n)-1;i>=0;i--)
#define rreps(i,n) for (int i=(n);i>0;i--)
#define all(v) (v).begin(),(v).end()
#define pi 3.1415926535897932384
#define E9 1000000000
#define eps 1e-4
#define pii pair<int,int>
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;};
typedef long long int ll;
const long long INF = 1LL << 60;

int main(){
  int N, K; cin >> N >> K;
  int A[N]; rep(i,N) cin >> A[i];

  int l=0,r=1000000000;
  while (abs(l-r)>1){
    int x = (l+r)/2;
    ll tmp = 0;
    rep(i,N) tmp += (A[i]-1)/x;
    if (tmp<=K) r = x;
    else l = x;
    // cout << l << " " << r << endl;
  }
  cout << r << endl;

  // cout << fixed << setprecision(10);

  return 0;
}

Score : 500 points

Problem Statement

We have N logs of lengths A_1,A_2,\cdots A_N.

We can cut these logs at most K times in total. When a log of length L is cut at a point whose distance from an end of the log is t (0<t<L), it becomes two logs of lengths t and L-t.

Find the shortest possible length of the longest log after at most K cuts, and print it after rounding up to an integer.

Constraints

1 \leq N \leq 2 \times 10^5

0 \leq K \leq 10^9

1 \leq A_i \leq 10^9

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N K
A_1 A_2 \cdots A_N

Output

Print an integer representing the answer.

Sample Input 1

2 3
7 9

Sample Output 1

4

First, we will cut the log of length 7 at a point whose distance from an end of the log is 3.5, resulting in two logs of length 3.5 each.

Next, we will cut the log of length 9 at a point whose distance from an end of the log is 3, resulting in two logs of length 3 and 6.

Lastly, we will cut the log of length 6 at a point whose distance from an end of the log is 3.3, resulting in two logs of length 3.3 and 2.7.

In this case, the longest length of a log will be 3.5, which is the shortest possible result. After rounding up to an integer, the output should be 4.

Sample Input 2

3 0
3 4 5

Sample Output 2

5

Sample Input 3

10 10
158260522 877914575 602436426 24979445 861648772 623690081 433933447 476190629 262703497 211047202

Sample Output 3

292638192


#include<bits/stdc++.h>
using namespace std;
int n,q,xl,xr,res;
int a[500010],cnt[500010],ans[500010];
struct Node{
	int l,r,num;
}f[500010];
inline bool cmp(Node a,Node b){
	if(a.l/800==b.l/800)
		return a.r<b.r;
	return a.l<b.l;
}
inline void Add(int x){
	if(cnt[x]==0)
		res++;
	cnt[x]++;
}
inline void Delete(int x){
	if(cnt[x]==1)
		res--;
	cnt[x]--;
}
int main(){
	cin>>n>>q;
	for(int i=1;i<=n;i++)
		cin>>a[i];
	for(int i=1;i<=q;i++){
		cin>>f[i].l>>f[i].r;
		f[i].num=i;
	}
	sort(f+1,f+q+1,cmp);
	xl=1,xr=1,res=1;
	cnt[a[1]]=1;
	for(int i=1;i<=q;i++){
		while(xl<f[i].l){
			Delete(a[xl]);
			xl++;
		}
		while(xl>f[i].l){
			xl--;
			Add(a[xl]);
		}
		while(xr<f[i].r){
			xr++;
			Add(a[xr]);
		}
		while(xr>f[i].r){
			Delete(a[xr]);
			xr--;
		}
		ans[f[i].num]=res;
	}
	for(int i=1;i<=q;i++)
		cout<<ans[i]<<endl;
	return 0;
}

Score : 600 points

Problem Statement

We have N colored balls arranged in a row from left to right; the color of the i-th ball from the left is c_i.

You are given Q queries. The i-th query is as follows: how many different colors do the l_i-th through r_i-th balls from the left have?

Constraints

1\leq N,Q \leq 5 \times 10^5

1\leq c_i \leq N

1\leq l_i \leq r_i \leq N

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N Q
c_1 c_2 \cdots c_N
l_1 r_1
l_2 r_2
:
l_Q r_Q

Output

Print Q lines. The i-th line should contain the response to the i-th query.

Sample Input 1

4 3
1 2 1 3
1 3
2 4
3 3

Sample Output 1

2
3
1

The 1-st, 2-nd, and 3-rd balls from the left have the colors 1, 2, and 1 - two different colors.

The 2-st, 3-rd, and 4-th balls from the left have the colors 2, 1, and 3 - three different colors.

The 3-rd ball from the left has the color 1 - just one color.

Sample Input 2

10 10
2 5 6 5 2 1 7 9 7 2
5 5
2 4
6 7
2 2
7 8
7 9
1 8
6 9
8 10
6 8

Sample Output 2

1
2
2
1
2
2
6
3
3
3


#pragma GCC optimize ("O3")
#include <bits/stdc++.h>
#define endl "\n"
#define sz(x) ((ll)(x).size())
#define pb push_back
#define all(x) (x).begin(),(x).end()
#define prt(x) cout << (x)
#define prtl(x) cout << (x) << endl
#define rep(i,a,b) for(ll i=a; i<b; i++)
#define rrep(i,a,b) for(ll i=a; i>b; i--)
#define mp(a, b) make_pair(a, b)
#define chmin(x, y) x = min(x, y)
#define chmax(x, y) x = max(x, y)
#define debug(v) cout << #v<< ": " << v <<endl;
#define fst first
#define snd second
using namespace std;
using vi = vector<int>;
using vll = vector<long long int>;
using vb = vector<bool>;
using vvb = vector<vector<bool>>;
using vc = vector<char>;
using vvi = vector<vector<int>>;
using vvll = vector<vector<long long int>>;
using vvc = vector<vector<char>>;
using ll = long long int;
using P = pair<long long int, long long int>;
using Map = map<long long int, long long int>;
ll INF = 1LL<<60;
ll M = 1000000007;

struct Mo
{
  vector< int > left, right, order;
  vector< bool > v;
  int width;
  int nl, nr, ptr;

  Mo(int n) : width((int) sqrt(n)), nl(0), nr(0), ptr(0), v(n) {}

  void insert(int l, int r) /* [l, r) */
  {
    left.push_back(l);
    right.push_back(r);
  }

  /* ソート */
  void build()
  {
    order.resize(left.size());
    iota(begin(order), end(order), 0);
    sort(begin(order), end(order), [&](int a, int b)
    {
      if(left[a] / width != left[b] / width) return left[a] < left[b];
      return right[a] < right[b];
    });
  }

  /* クエリを 1 つぶんすすめて, クエリのidを返す */
  int process()
  {
    if(ptr == order.size()) return (-1);
    const auto id = order[ptr];
    while(nl > left[id]) distribute(--nl);
    while(nr < right[id]) distribute(nr++);
    while(nl < left[id]) distribute(nl++);
    while(nr > right[id]) distribute(--nr);
    return (order[ptr++]);
  }

  inline void distribute(int idx)
  {
    v[idx].flip();
    if(v[idx]) add(idx);
    else del(idx);
  }

  void add(int idx);

  void del(int idx);
};

int N, A[500001], Q;
int ans[500001];
int cnt[500001], sum;

void Mo::add(int idx)
{
  if(cnt[A[idx]]++ == 0) ++sum;
}

void Mo::del(int idx)
{
  if(--cnt[A[idx]] == 0) --sum;
}

int main()
{
  cin >> N;
  cin >> Q;
  for(int i = 0; i < N; i++) {
    cin >> A[i];
  }
  Mo mo(N);
  for(int i = 0; i < Q; i++) {
    int a, b;
    cin >> a >> b;
    mo.insert(--a, b);
  }
  mo.build();
  for(int i = 0; i < Q; i++) {
      int g = mo.process();
       ans[g] = sum;
  }
  for(int i = 0; i < Q; i++) {
    prtl(ans[i]);
  }
}


#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>

struct event {
	struct QueryEvent *qe;
	struct NumberEvent *ne;
	int type;
};

struct QueryEvent {
	int i, j, ans;
};

struct NumberEvent {
	int num, ind;
};

struct node {
	int l, r, v;
};

int reg[1000001];

struct node *root;

int cmpFun(const void *a, const void *b){
	struct event *f = (struct event *)a;
	struct event *s = (struct event *)b;
	int r = 0;
	if(f->type==1){
		if(s->type==1){
			r = f->ne->ind - s->ne->ind;
		}else{
			r = f->ne->ind - s->qe->j;
			if(r==0)r=-1;
		}
	}else if(f->type==2){
		if(s->type==1){
			r = f->qe->j - s->ne->ind;
			if(r==0)r = 1;
		}else{
			r = f->qe->j - s->qe->j;
		}
	}
	return r;
}

int insert(int start, int end, int index, int pos){
	if(start==end){
		root[index].l = 0;
		root[index].r = 0;
		root[index].v = 1;
		return 1;
	}

	int mid = (start+end)/2;

	if(pos <= mid){
		int l = insert(start, mid, index*2+1, pos);
		root[index].l = l;
	}else{
		int r = insert(mid+1, end, index*2+2, pos);
		root[index].r = r;
	}

	return root[index].v = root[index].l + root[index].r;
}

int removee(int start, int end, int del, int index){
	int pos = del;
	if(start==end){
		root[index].l = 0;
		root[index].r = 0;
		root[index].v = 0;
		return 0;
	}

	int mid = (start+end)/2;

	if(pos <= mid){
		int l = removee(start, mid, del, index*2+1);
		root[index].l = l;
	}else{
		int r = removee(mid+1, end, del, index*2+2);
		root[index].r = r;
	}

	return root[index].v = root[index].l + root[index].r;
}

int query(int start, int end, int qi, int qj, int index){
	if(start>=qi && end <= qj){
		return root[index].v;
	}

	if(qj<start || qi>end){
		return 0;
	}

	int mid = (start+end)/2;
	return query(start, mid, qi, qj, index*2+1)
		+  query(mid+1, end, qi, qj, index*2+2);
}

int main()
{
	memset(reg, -1, 1000001*sizeof(int));
	int n, q;
	struct event *events;
	scanf("%d", &n);
	scanf("%d", &q);

	root = (struct node*)malloc(sizeof(struct node)*(2*pow(2, ceil(log((double)n)/log((double)2)))));

	events = (struct event *)malloc(sizeof(struct event)*(n+200000));
	int ind=0;
	for(int i=0;i<n;++i){
		struct event *e = events+ind++;
		struct NumberEvent *ne = (struct NumberEvent *)malloc(sizeof(struct NumberEvent));
		scanf("%d", &ne->num);
		ne->ind = i;
		e->ne = ne;
		e->type = 1;
	}

	struct QueryEvent **original_Queries = (struct QueryEvent **)malloc(q*sizeof(struct QueryEvent*));

	for(int i=0;i<q;++i){
		struct event *q = events+ind++;
		struct QueryEvent *e = (struct QueryEvent *)malloc(sizeof(struct QueryEvent));
		int a, b;
		scanf("%d%d", &a, &b);
		q->type = 2;
		e->i = a-1;
		e->j = b-1;
		q->qe = e;
		original_Queries[i] = e;
	}

	qsort(events, ind, sizeof(struct event), cmpFun);

	for(int i=0;i<ind;++i){
		struct event *e = events+i;
		if(e->type==1){
			//Number Event
			if(reg[e->ne->num]==-1){
				//Not in tree, add
				insert(0, n-1, 0, e->ne->ind);
				reg[e->ne->num] = e->ne->ind;
			}else{
				int del = reg[e->ne->num];
				//delete this from seg tree
				removee(0, n-1, del, 0);
				reg[e->ne->num] = e->ne->ind;
				//Add this
				insert(0, n-1, 0, e->ne->ind);
			}
		}else{
			//Query Event
			int i = e->qe->i;
			int j = e->qe->j;
			int ans = query(0, n-1, i, j, 0);
			e->qe->ans = ans;
		}
	}

	for(int i=0;i<q;++i){
		printf("%d\n", original_Queries[i]->ans);
	}
	return 0;
}


#include<iostream>
using namespace std;
int main(){
    int A,B,C,K;
    cin >> A >> B >> C >> K;
    int k = 0;
    while(A>=B){
        if(k==K) break;
        B = B * 2;
        k++;
    }
    while(B>=C){
        if(k==K) break;
        C = C * 2;
        k++;
    }
    if(A<B&&B<C){
        cout << "Yes";
    }
    else{
        cout << "No";
    }
}

Score: 200 points

Problem Statement

M-kun has the following three cards:

A red card with the integer A.

A green card with the integer B.

A blue card with the integer C.

He is a genius magician who can do the following operation at most K times:

Choose one of the three cards and multiply the written integer by 2.

His magic is successful if both of the following conditions are satisfied after the operations:

The integer on the green card is strictly greater than the integer on the red card.

The integer on the blue card is strictly greater than the integer on the green card.

Determine whether the magic can be successful.

Constraints

1 \leq A, B, C \leq 7

1 \leq K \leq 7

All values in input are integers.

Input

Input is given from Standard Input in the following format:

A B C
K

Output

If the magic can be successful, print Yes; otherwise, print No.

Sample Input 1

7 2 5
3

Sample Output 1

Yes

The magic will be successful if, for example, he does the following operations:

First, choose the blue card. The integers on the red, green, and blue cards are now 7, 2, and 10, respectively.

Second, choose the green card. The integers on the red, green, and blue cards are now 7, 4, and 10, respectively.

Third, choose the green card. The integers on the red, green, and blue cards are now 7, 8, and 10, respectively.

Sample Input 2

7 4 2
3

Sample Output 2

No

He has no way to succeed in the magic with at most three operations.


#include<bits/stdc++.h>
#define Imposter ios_base::sync_with_stdio(false);cin.tie(NULL);
#define pb push_back
#define ll long long int
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#pragma GCC optimize("O3")
using namespace __gnu_pbds;
#define ordered_set tree<ll, null_type,less_equal<ll>, rb_tree_tag,tree_order_statistics_node_update>//s.order_of_key(val) *s.find_by_order(ind)
using namespace std;
#define f(n) for(ll i=0;i<n;i++)
#define rep(i,a,b) for(i=a;i<b;i++)
#define input(arr,n) for(ll i1=0;i1<n;i1++ )cin>>arr[i1]
#define vll vector<ll>
#define vlll vector<vll>
#define all(x) x.begin(),x.end()
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;
}
int main()
{
    Imposter

    int t=1;
    //cin>>t;
    while(t--)
    {
       ll n,k;
       cin>>n>>k;
       ll a[n];
       input(a,n);
       deque<ll>ans;
       f(k)
       {
           ans.push_back(a[i]);
       }
       for(int i=k;i<n;i++)
       {
           if(ans.front()>=a[i])
           cout<<"No"<<endl;
           else
           cout<<"Yes"<<endl;
           ans.pop_front();
           ans.push_back(a[i]);
       }

    }
    return 0;
}

Score: 300 points

Problem Statement

M-kun is a student in Aoki High School, where a year is divided into N terms.

There is an exam at the end of each term. According to the scores in those exams, a student is given a grade for each term, as follows:

For the first through (K-1)-th terms: not given.

For each of the K-th through N-th terms: the multiplication of the scores in the last K exams, including the exam in the graded term.

M-kun scored A_i in the exam at the end of the i-th term.

For each i such that K+1 \leq i \leq N, determine whether his grade for the i-th term is strictly greater than the grade for the (i-1)-th term.

Constraints

2 \leq N \leq 200000

1 \leq K \leq N-1

1 \leq A_i \leq 10^{9}

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N K
A_1 A_2 A_3 \ldots A_N

Output

Print the answer in N-K lines.

The i-th line should contain Yes if the grade for the (K+i)-th term is greater than the grade for the (K+i-1)-th term, and No otherwise.

Sample Input 1

5 3
96 98 95 100 20

Sample Output 1

Yes
No

His grade for each term is computed as follows:

3-rd term: (96 \times 98 \times 95) = 893760

4-th term: (98 \times 95 \times 100) = 931000

5-th term: (95 \times 100 \times 20) = 190000

Sample Input 2

3 2
1001 869120 1001

Sample Output 2

No

Note that the output should be No if the grade for the 3-rd term is equal to the grade for the 2-nd term.

Sample Input 3

15 7
3 1 4 1 5 9 2 6 5 3 5 8 9 7 9

Sample Output 3

Yes
Yes
No
Yes
Yes
No
Yes
Yes


#include <bits/stdc++.h>

using namespace std;

#define endl "\n"
#define int long long
#define test int qt;cin>>qt;while (qt--)
#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0)

#define all(c) (c).begin(), (c).end()
#define sz(x) (int)(x).size()

#define F_OR(i, a, b, s) for (int i=(a); (s)>0?i<(b):i>(b); i+=(s))
#define F_OR1(e) F_OR(i, 0, e, 1)
#define F_OR2(i, e) F_OR(i, 0, e, 1)
#define F_OR3(i, b, e) F_OR(i, b, e, 1)
#define F_OR4(i, b, e, s) F_OR(i, b, e, s)
#define GET5(a, b, c, d, e, ...) e
#define F_ORC(...) GET5(__VA_ARGS__, F_OR4, F_OR3, F_OR2, F_OR1)
#define FOR(...) F_ORC(__VA_ARGS__)(__VA_ARGS__)
#define EACH(x, a) for (auto& x: a)

int32_t main()
{
    IOS;
    int n,k;cin>>n>>k;
    int t=k;
    int a[n];EACH(x,a)cin>>x;
    vector<int>ans;
    FOR(i,k-1,n) {
        int temp=1;
        while(t--) {
            temp*=a[i-t];

        }
        ans.push_back(temp);
        t=k;
    }
    FOR(i,1,ans.size()) {
        if(ans[i]>ans[i-1])cout<<"Yes"<<endl;
        else cout<<"No"<<endl;
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
//#include<ext/pb_ds/assoc_container.hpp>
//#include<ext/pb_ds/tree_policy.hpp>
//
//    #pragma GCC optimize("unroll-loops")
//    #pragma GCC optimize("Ofast")
//    #pragma GCC optimize("-O3")
//    #pragma GCC optimize("no-stack-protector")
//    #pragma GCC optimize("fast-math")
//#define LOCAL
#define sim template < class c
#define ris return * this
#define dor > debug & operator <<
#define eni(x) sim > typename \
  enable_if<sizeof dud<c>(0) x 1, debug&>::type operator<<(c i) {
sim > struct rge { c b, e; };
sim > rge<c> range(c i, c j) { return rge<c>{i, j}; }
sim > auto dud(c* x) -> decltype(cerr << *x, 0);
sim > char dud(...);
struct debug {
#ifndef LOCAL
~debug() { cerr << endl; }
eni(!=) cerr << boolalpha << i; ris; }
eni(==) ris << range(begin(i), end(i)); }
sim, class b dor(pair < b, c > d) {
  ris << "(" << d.first << ", " << d.second << ")";
}
sim dor(rge<c> d) {
  *this << "[";
  for (auto it = d.b; it != d.e; ++it)
	*this << ", " + 2 * (it == d.b) << *it;
  ris << "]";
}
#else
sim dor(const c&) { ris; }
#endif
};
#define imie(...) " [" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "] "
#define fi first
#define f first
#define se second
#define s second
#define vi_a vector<int>a;
#define p_b push_back
////////////////////////////////???????????????CHECK THIS OUT???????????????//////////////////////////////
#define ll long long
typedef unsigned long long ull;
////////////////////////////////???????????????CHECK THIS OUT???????????????//////////////////////////////
#define ld long double
#define pll pair<ll,ll>
#define pii pair<int,int>
#define m_p make_pair
#define fast_io cin.tie(0);cout.tie(0);ios_base::sync_with_stdio(0);
#define all(x) x.begin(),x.end()
#define getfiles    ifstream cin("input.txt");ofstream cout("output.txt");
#define pw(x) (1ll << x)
#define sz(x) (int)x.size()
#define endl "\n"
#define rall(x) x.rbegin(),x.rend()
#define len(a) (ll)a.size()
#define rep(x,l,r) for(ll x=l;x<r;x++)

//using namespace __gnu_pbds;
ld eps = (ld)1 / 1e6;
const ld pi=3.14159265359;
ll inf = 1e18,mod1=1e9+7;
ll sqr(ll a) { return a * a; }
ll qb(ll a) { return a * a * a; }
ll gcd(ll a, ll b) { return !a ? b : gcd(b % a, a); }
ll binpow(ll a, ll b, ll mod) { return b ? (b % 2 ? (a * (sqr(binpow(a, b / 2, mod)) % mod)) % mod : sqr(binpow(a, b / 2, mod)) % mod) : 1; }
ll binmult(ll a, ll b, ll mod) { return b ? (b % 2 ? (2 * binmult(a, b / 2, mod) + a) % mod : (2 * binmult(a, b / 2, mod)) % mod) : 0; }
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
const ll R=1e4;
const ll tx[4]={0,0,-1,1};
const ll ty[4]={-1,1,0,0};
const char rev_to[4]={'E','W','N','S'};
const int N=2e3+1;
const int M=1e9+7;
//typedef tree<ll,null_type,less<ll>,rb_tree_tag,tree_order_statistics_node_update> st;
auto rnd=bind(uniform_int_distribution<ll>(1,125),mt19937(time(0)));
void bad(){
    cout<<-1;
    exit(0);
}
signed main()
{
    fast_io;
    ll n,k;
    cin>>n>>k;
    vector<ll>a(n);
    for(auto &z : a) cin>>z;
    vector<ll>p(n);
    for(int i=0;i<n;i++){
        p[i]=(i? p[i-1] : 1)*a[i];
    }
    for(int i=k;i<n;i++){
        ll f=p[i]/(i-k>=0 ? p[i-k] : 1),s=p[i-1]/(i-k-1>=0 ? p[i-k-1] : 1);
        debug()<<imie(f)<<imie(s);
        cout<<(f>s ? "Yes" : "No")<<endl;
    }
    return 0;
}
/*
0 0 0
2 1 3
3 2 7
1 3 8
1 4 2
3 5 3

/
*/


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

#define INF 2147483647
#define LINF 9223372036854775807
#define MOD 1000000007
#define MOD2 998244353

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

#define rep(i,n) for(int i=0,_i=(n);i<_i;++i)
#define rep1(i,a,b) for(int a_=(a),b_=(b),i=a_;i<b_;++i)
#define repr(i,n) for(int _i=(n),i=_i;i>0;--i)

#define db(x) cerr<<#x<<" = "<<x<<" ";
#define db2(x,y) cerr<<"("<<#x<<", "<<#y<<") = ("<<x<<", "<<y<<") ";
#define db3(x,y,z) cerr<<"("<<#x<<", "<<#y<<", "<<#z<<") = ("<<x<<", "<<y<<", "<<z<<") ";
#define ln cout<<endl;

#define all(a)  (a).begin(),(a).end()
#define dig(n)  to_string(n).length()

#define pb push_back
#define eb emplace_back
#define mp make_pair
#define se second
#define fi first

typedef long long ll;
typedef long double ld;

typedef pair<int,int> P;
typedef pair<int,P> iP;
typedef pair<P,P> PP;

ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
ll lcm(ll a,ll b){return (a*b)/gcd(a,b);}

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

int main(){

    bool flag=false;
    ll ans=0,sum=0;

    int n;
    cin>>n;
    vector<int> a(n+1,0);
    a[n] = -1;

    rep(i,n)cin>>a[i];

    int p = 0;
    int mode = 1;

    int g=1000,k=0;

    while(1){
        if(mode == 1){

            while(a[p] >= a[p+1] && p<n)++p;
            mode = 0;

            if(p >= n)break;
            k += g/a[p];
            g -= (g/a[p])*a[p];

        } else {

            while(a[p] <= a[p+1] && p<n)++p;
            mode = 1;

            if(p >= n)break;
            g += k*a[p];
            k = 0;
        }

        //cout<<"day "<<p+1;
        //db2(g,k);ln;

        ++p;
    }

    cout<<g<<endl;
    //cout <<fixed<<setprecision(16)<< << endl;
    //if(flag)cout << "Yes" <<endl;
    //else cout << "No" <<endl;

    return 0;
}

Score: 400 points

Problem Statement

To become a millionaire, M-kun has decided to make money by trading in the next N days. Currently, he has 1000 yen and no stocks - only one kind of stock is issued in the country where he lives.

He is famous across the country for his ability to foresee the future. He already knows that the price of one stock in the next N days will be as follows:

A_1 yen on the 1-st day, A_2 yen on the 2-nd day, ..., A_N yen on the N-th day.

In the i-th day, M-kun can make the following trade any number of times (possibly zero), within the amount of money and stocks that he has at the time.

Buy stock: Pay A_i yen and receive one stock.

Sell stock: Sell one stock for A_i yen.

What is the maximum possible amount of money that M-kun can have in the end by trading optimally?

Constraints

2 \leq N \leq 80

100 \leq A_i \leq 200

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
A_1 A_2 \cdots A_N

Output

Print the maximum possible amount of money that M-kun can have in the end, as an integer.

Sample Input 1

7
100 130 130 130 115 115 150

Sample Output 1

1685

In this sample input, M-kun has seven days of trading. One way to have 1685 yen in the end is as follows:

Initially, he has 1000 yen and no stocks.

Day 1: Buy 10 stocks for 1000 yen. Now he has 0 yen.

Day 2: Sell 7 stocks for 910 yen. Now he has 910 yen.

Day 3: Sell 3 stocks for 390 yen. Now he has 1300 yen.

Day 4: Do nothing.

Day 5: Buy 1 stock for 115 yen. Now he has 1185 yen.

Day 6: Buy 10 stocks for 1150 yen. Now he has 35 yen.

Day 7: Sell 11 stocks for 1650 yen. Now he has 1685 yen.

There is no way to have 1686 yen or more in the end, so the answer is 1685.

Sample Input 2

6
200 180 160 140 120 100

Sample Output 2

1000

In this sample input, it is optimal to do nothing throughout the six days, after which we will have 1000 yen.

Sample Input 3

2
157 193

Sample Output 3

1216

In this sample input, it is optimal to buy 6 stocks in Day 1 and sell them in Day 2, after which we will have 1216 yen.


#include <iostream>
#include<vector>
using namespace std;
int main(void){
    int n;
    cin >> n;
    vector<int> a(n);
    long money = 1000;
    long stock = 0;
    for(int i = 0;i < n;i++){
          cin >> a[i];
    }

    for(int i = 0;i < n - 1;i++){
          if(a[i] < a[i + 1]){
                stock = money / a[i];
                money -= a[i] * stock;
                money += stock * a[i + 1];
          }
    }
    cout << money << endl;
}


#include<bits/stdc++.h>
#define ll long long
using namespace std;
ll a[200005];
int main()
{
	int n,sum=1000,num=0;cin>>n;
	for(int i=1;i<=n;++i)
	cin>>a[i];
	for(int i=2;i<=n+1;++i)
	{
		if(a[i]>a[i-1])
		{
			num=sum/a[i-1];
			sum=sum-num*a[i-1];
		}
		 if(a[i]<a[i-1])
		{
			sum=sum+num*a[i-1];num=0;
		}
	}
	cout<<sum<<endl;
}


#include<bits/stdc++.h>
using namespace std;
template <typename T>
inline void readin(T &x) {
	x = 0;
	T fh = 1;
	char ch = getchar();
	for (; !isdigit(ch); ch = getchar()) if (ch == '-') fh = -1;
	for (; isdigit(ch); ch = getchar()) x = (x << 1) + (x << 3) + (ch ^ 48);
	x *= fh;
}
inline void d_read(double &x) {
	x = 0.0;
	int fh = 1;
	char ch = getchar();
	for (; !isdigit(ch); ch = getchar()) if (ch == '-') fh = -1;
	for (; isdigit(ch); ch = getchar()) x = x * 10 + (ch ^ 48);
	if (ch == '.') {
		double num = 1.0;
		ch = getchar();
		for (; isdigit(ch); ch = getchar()) x = x + (num /= 10) * (ch ^ 48);
	}
	x *= fh;
}

template <typename T>
inline void wt(T x) {
	if (x > 9) wt(x / 10);
	putchar(x % 10 + 48);
}
template <typename T>
inline void writeln(T x, char c) {
	if (x < 0) {
		putchar('-');
		x = -x;
	}
	wt(x);
	putchar(c);
}
const int N = 105;
int a[N];
int main() {
	int n;
	readin(n);
	for (int i = 1; i <= n; i ++) {
		readin(a[i]);
	}
	int res = 1000, st = 0;
	a[0] = 1e9;
	for (int i = 1; i <= n; i ++) {
		if (a[i] >= a[i - 1] && a[i] > a[i + 1]) {
			res += st * a[i];
			st = 0;
		}
		else if (a[i] <= a[i - 1] && a[i] < a[i + 1]) {
			st = res / a[i];
			res = res - st * a[i];
		}
	}
	writeln(res, '\n');
	return 0;
}


#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 INF 2e9
#define ALL(v) v.begin(), v.end()
using namespace std;
typedef long long ll;

int main()
{
    ll L, R, d;
    cin >> L >> R >> d;
    ll ans = 0;
    FOR(i, L, R+1) {
        if (i % d == 0) ans++;
    }
    cout << ans << endl;
}

Score : 100 points

Problem Statement

How many multiples of d are there among the integers between L and R (inclusive)?

Constraints

All values in input are integers.

1 \leq L \leq R \leq 100

1 \leq d \leq 100

Input

Input is given from Standard Input in the following format:

L R d

Output

Print the number of multiples of d among the integers between L and R (inclusive).

Sample Input 1

5 10 2

Sample Output 1

3

Among the integers between 5 and 10, there are three multiples of 2: 6, 8, and 10.

Sample Input 2

6 20 7

Sample Output 2

2

Among the integers between 6 and 20, there are two multiples of 7: 7 and 14.

Sample Input 3

1 100 1

Sample Output 3

100


#include <bits/stdc++.h>
#include <ext/pb_ds/detail/standard_policies.hpp>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

#define pb push_back
#define F first
#define S second
#define ll long long
#define ull unsigned long long
#define ld long double
#define endl '\n'
#define TIME 1.0*clock()/CLOCKS_PER_SEC
#define pii pair < int , int >
#define Endl '\n'

#pragma GCC optimize("Ofast")

using namespace std;
using namespace __gnu_pbds;

template <typename T> using ordered_set = tree <T,null_type,less<T>,rb_tree_tag,tree_order_statistics_node_update>;

mt19937 gen(chrono::system_clock::now().time_since_epoch().count());

const int mod = 1e9 + 7;
const int FFTM = 998244353;

const int SX[4] = {0 , 1 , -1 , 0};
const int SY[4] = {1 , 0 , 0 , -1};
const int rx[8] = {1, -1, 0, 0, 1, 1, -1, -1};
const int ry[8] = {0, 0, 1, -1, 1, -1, 1, -1};
const int kx[8] = {1, 1, -1, -1, 2, 2, -2, -2};
const int ky[8] = {2, -2, 2, -2, 1, -1, 1, -1};

typedef cc_hash_table< int, int, hash<int>, equal_to<int>, direct_mask_range_hashing<int>, hash_standard_resize_policy<hash_exponential_size_policy<>, hash_load_check_resize_trigger<true>, true>> ht;

int32_t main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
#ifdef LOCAL
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#else
//    freopen("gotham.in", "r", stdin);
//    freopen("gotham.out", "w", stdout);
#endif
    int l, r, d;
    cin >> l >> r >> d;
    int kol = 0;
    for (int i = l;i <= r; ++i){
        if (i % d == 0)kol++;
    }
    cout << kol << endl;
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
using pint = pair<int, int>;
using ll = long long;
using ull = unsigned long long;
using vll = vector<long long>;
using pll = pair<ll, ll>;
#define FOR(i, begin, end) \
    for (int i = (begin), i##_end_ = (end); i < i##_end_; i++)
#define IFOR(i, begin, end) \
    for (int i = (end)-1, i##_begin_ = (begin); i >= i##_begin_; i--)
#define REP(i, n) FOR(i, 0, n)
#define IREP(i, n) IFOR(i, 0, n)
#define VREP(s, ite) for (auto ite = s.begin(); ite != s.end(); ++ite)
#define FI first
#define SE second
#define ALL(v) v.begin(), v.end()
//#define endl "\n"
#define ciosup  \
    cin.tie(0); \
    ios::sync_with_stdio(false);
#define eb emplace_back
#define vint vector<int>
constexpr ll INF = 1e15 + 7LL;
constexpr ll MOD = 1e9 + 7LL;
template <typename T>
istream &operator>>(istream &is, vector<T> &v) {
    for (int i = 0; i < v.size(); ++i) {
        is >> v[i];
    }
    return is;
}
template <typename T>
ostream &operator<<(ostream &os, const vector<T> &v) {
    for (int i = 0; i < v.size() - 1; ++i) {
        os << v[i] << " ";
    }
    if (v.size() > 0) {
        os << v[v.size() - 1] << endl;
    }
    return os;
}

int main() {
    int n;
    cin>>n;
    vint a(n);
    cin>>a;
    int ans = 0;
    REP(i, n) {
        if ((i + 1) % 2 == 1 && a[i] % 2 == 1){
            ++ans;
        }
    }
    cout << ans << endl;
}

Score : 200 points

Problem Statement

We have N squares assigned the numbers 1,2,3,\ldots,N. Each square has an integer written on it, and the integer written on Square i is a_i.

How many squares i satisfy both of the following conditions?

The assigned number, i, is odd.

The written integer is odd.

Constraints

All values in input are integers.

1 \leq N, a_i \leq 100

Input

Input is given from Standard Input in the following format:

N
a_1 a_2 \cdots a_N

Output

Print the number of squares that satisfy both of the conditions.

Sample Input 1

5
1 3 4 5 7

Sample Output 1

2

Two squares, Square 1 and 5, satisfy both of the conditions.

For Square 2 and 4, the assigned numbers are not odd.

For Square 3, the written integer is not odd.

Sample Input 2

15
13 76 46 15 50 98 93 77 31 43 84 90 6 24 14

Sample Output 2

3


#include<bits/stdc++.h>
 using namespace std;
 #define ll long long int
 int main()
  {   ios_base::sync_with_stdio(false);
    cin.tie(NULL);
      ll c,d,e,f,g,r,i,x=0,sum,l,v,aa,bb,y;
     ll n;
      cin>>n;
     string s,t;
      cin>>s;
       t=s;
        sum=0;
       reverse(t.begin(),t.end());
       for(i=0;i<n;i++)
       { if(t[i]=='1')
            sum+=pow(2,i);
       }
        for(i=0;i<n;i++)
        {    c=sum;
              d=0;
            if(s[i]=='0')
           c+=1LL<<(n-i-1);
              else
           c-=1LL<<(n-i-1);
             while(c)
             { c=c%(__builtin_popcount(c));
                 d++;
             }
             cout<<d<<endl;
        }

  }

Score : 400 points

Problem Statement

Let \mathrm{popcount}(n) be the number of 1s in the binary representation of n.
For example, \mathrm{popcount}(3) = 2, \mathrm{popcount}(7) = 3, and \mathrm{popcount}(0) = 0.

Let f(n) be the number of times the following operation will be done when we repeat it until n becomes 0: "replace n with the remainder when n is divided by \mathrm{popcount}(n)." (It can be proved that, under the constraints of this problem, n always becomes 0 after a finite number of operations.)

For example, when n=7, it becomes 0 after two operations, as follows:

\mathrm{popcount}(7)=3, so we divide 7 by 3 and replace it with the remainder, 1.

\mathrm{popcount}(1)=1, so we divide 1 by 1 and replace it with the remainder, 0.

You are given an integer X with N digits in binary.
For each integer i such that 1 \leq i \leq N, let X_i be what X becomes when the i-th bit from the top is inverted.
Find f(X_1), f(X_2), \ldots, f(X_N).

Constraints

1 \leq N \leq 2 \times 10^5

X is an integer with N digits in binary, possibly with leading zeros.

Input

Input is given from Standard Input in the following format:

N
X

Output

Print N lines. The i-th line should contain the value f(X_i).

Sample Input 1

3
011

Sample Output 1

2
1
1

X_1 = 7, which will change as follows: 7 \rightarrow 1 \rightarrow 0. Thus, f(7) = 2.

X_2 = 1, which will change as follows: 1 \rightarrow 0. Thus, f(1) = 1.

X_3 = 2, which will change as follows: 2 \rightarrow 0. Thus, f(2) = 1.

Sample Input 2

23
00110111001011011001110

Sample Output 2

2
1
2
2
1
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
1
3


#include "bits/stdc++.h"
#include<random>
using namespace std;
typedef long long int lint;
typedef pair<lint, lint> plint;
typedef pair<double long, double long> pld;
#define ALL(x) (x).begin(), (x).end()
#define SZ(x) ((lint)(x).size())
#define POW2(n) (1LL << (n))
#define FOR(i, begin, end) for(lint i=(begin),i##_end_=(end);i<i##_end_;i++)
#define IFOR(i, begin, end) for(lint i=(end)-1,i##_begin_=(begin);i>=i##_begin_;i--)
#define REP(i, n) FOR(i,0,n)
#define IREP(i, n) IFOR(i,0,n)
template<class T>bool chmax(T& a, const T& b) { if (a < b) { a = b; return 1; } return 0; }
template<class T>bool chmin(T& a, const T& b) { if (b < a) { a = b; return 1; } return 0; }
template<typename T1, typename T2> pair<T1, T2> operator+(const pair<T1, T2>& l, const pair<T1, T2>& r) { return make_pair(l.first + r.first, l.second + r.second); }
template<typename T1, typename T2> pair<T1, T2> operator-(const pair<T1, T2>& l, const pair<T1, T2>& r) { return make_pair(l.first - r.first, l.second - r.second); }
const lint MOD = 1e9 + 7, INF = 1e16;

lint dpL[200001][3], dpR[200001][3];
lint dp2[200001][3];
lint N, cnt = 0;
string s;
lint dp3[200001];

lint dfs(lint i) {
	if (dp3[i] != INF) return dp3[i];
	lint b_cnt = 0, curr = i;
	while (curr > 0) {
		if (curr % 2 == 1) b_cnt++;
		curr /= 2;
	}
	return dp3[i] = dfs(i % b_cnt) + 1;
}

int main() {
	cin >> N;
	cin >> s;
	REP(i, N) {
		if (s[i] == '1') cnt++;
	}
	REP(i, 3) {
		dpL[0][i] = 0;
		dpR[N][i] = 0;
	}
	REP(i, N) {
		dpL[i + 1][0] = (dpL[i][0] * 2 + (s[i] - '0')) % (cnt - 1);
		dpL[i + 1][1] = (dpL[i][1] * 2 + (s[i] - '0')) % cnt;
		dpL[i + 1][2] = (dpL[i][2] * 2 + (s[i] - '0')) % (cnt + 1);
	}
	dp2[N][0] = 1 % (cnt - 1);
	dp2[N][1] = 1 % (cnt - 0);
	dp2[N][2] = 1 % (cnt + 1);
	IREP(i, N) {
		dp2[i][0] = (dp2[i + 1][0] * 2) % (cnt - 1);
		dp2[i][1] = (dp2[i + 1][1] * 2) % (cnt);
		dp2[i][2] = (dp2[i + 1][2] * 2) % (cnt + 1);
	}
	IREP(i, N) {
		dpR[i][0] = (dpR[i + 1][0] + (s[i] - '0') * dp2[i + 1][0]) % (cnt - 1);
		dpR[i][1] = (dpR[i + 1][1] + (s[i] - '0') * dp2[i + 1][1]) % cnt;
		dpR[i][2] = (dpR[i + 1][2] + (s[i] - '0') * dp2[i + 1][2]) % (cnt + 1);
	}
	REP(i, N) {
		dp3[i] = INF;
	}
	dp3[0] = 0;
	REP(i, N + 1) {
		dfs(i);
	}
	REP(i, N) {
		if (s[i] == '0') {
			int a = (dpL[i + 1][2] * dp2[i+1][2] + dp2[i + 1][2] + dpR[i + 1][2]) % (cnt + 1);
			cout << dp3[a] + 1 << endl;
		}
		else {
			int a = (dpL[i][0] * dp2[i][0] + dpR[i + 1][0]) % (cnt - 1);
			cout << dp3[a] + 1 << endl;

		}
	}
}


#include<bits/stdc++.h>
#define F first
#define S second
#define ll long long
#define MOD 1000000007
#define fast ios_base::sync_with_stdio(false)
#define itt cin.tie(0)
#define now cout.tie(0)
#define N 200010
using namespace std;
ll n,nn[2],md[N][2];
string s;
ll f(ll v,ll nd,ll sd,ll sx){
	ll ret=1,x=(nn[nd]+sd+sx*md[v][nd])%sd;
	while(x!=0)
	{
		x=x%__builtin_popcount(x);
		ret++;
	}
	return ret;
}
int main(){
	ll i,bb=0;
	fast;itt;now;
	cin>>n>>s;
	for(i=0;i<n;i++)
	{
		if(s[i]=='1')
		{
			bb++;
		}
	}
	md[0][0]=1%(bb+1);
	md[0][1]=1%(bb-1);
	for(i=1;i<=n;i++)
	{
		md[i][0]=(md[i-1][0]*2)%(bb+1);
		md[i][1]=(md[i-1][1]*2)%(bb-1);
	}
	nn[0]=nn[1]=0;
	for(i=0;i<n;i++)
	{
		nn[0]=(nn[0]*2+(s[i]-'0'))%(bb+1);
		nn[1]=(nn[1]*2+(s[i]-'0'))%(bb-1);
	}
	for(i=0;i<n;i++)
	{
		if(s[i]=='0')
		{
			cout<<f(n-i-1,0,bb+1,1)<<endl;
		}
		else
		{
			cout<<f(n-i-1,1,bb-1,-1)<<endl;
		}
	}
	return 0;
}


#include <bits/stdc++.h>
#include <stdio.h>
using namespace std;  //これで"std::cout"が"cout"と書ける
#pragma GCC optimize("Ofast")

//MACRO
typedef long long lint;                //64bit以上を表現可能な型
#define INF 100000000                  //ほぼ無限
#define itn int                        //タイポ対策
#define all(x) (x).begin(), (x).end()  //sort(all(x))などとする
#define SZ(x) ((int)(x).size())        //size()簡略化
//repetition
#define FOR(i, p, b) for (int i = (p); i < (b); ++i)
#define rep(i, n) FOR(i, 0, n)
#define repR(i, n) for (int i = n; i >= 0; i--)  //リバース
//debug
#define dump(x) cerr << #x << " = " << (x) << endl;
#define debug(x) cerr << #x << " = " << (x) << " (L" << __LINE__ << ")" \<< " " << __FILE__ << endl;
template <class T>
bool chmax(T &m, const T &b) {  //aよりbが大きければaに代入(参照渡し)
    if (m < b) {
        m = b;
        return 1;
    }
    return 0;
}
template <class T>
bool chmin(T &m, const T &b) {  //aよりbが小さければaに代入//参照渡し
    if (b < m) {
        m = b;
        return 1;
    }
    return 0;
}

int ctoi(const char c) {  //CtoI(int) charを渡すとintに変換する、数字以外だと-1を返す
    if ('0' <= c && c <= '9') return (c - '0');
    return -1;
}

class range {
   private:
    struct I {
        int x;
        int operator*() { return x; }
        bool operator!=(I &lhs) { return x < lhs.x; }
        void operator++() { ++x; }
    };
    I i, n;

   public:
    range(int n) : i({0}), n({n}) {}
    range(int i, int n) : i({i}), n({n}) {}
    I &begin() { return i; }
    I &end() { return n; }
};

template <typename T>
std::string to_string(const T &n) {
    std::ostringstream stm;
    stm << n;
    return stm.str();
}

template <typename List>
/*
好きな区切り文字でsplit
引数:対象の文字列,区切り文字,結果を格納する可変長配列
*/
void split(const std::string &s, const std::string &delim, List &result) {
    result.clear();

    using string = std::string;
    string::size_type pos = 0;

    while (pos != string::npos) {
        string::size_type p = s.find(delim, pos);

        if (p == string::npos) {
            result.push_back(s.substr(pos));
            break;
        } else {
            result.push_back(s.substr(pos, p - pos));
        }

        pos = p + delim.size();
    }
}

// vectorからindex番目の要素を削除する
template <typename T>
void remove(std::vector<T> &vector, unsigned int index) {
    vector.erase(vector.begin() + index);
}

/*エラトステネスのふるい
vector<bool> num(100000 + 2, true);
num[0] = false;
num[1] = false;

FOR(i, 2, SZ(num)) {
    if (num[i]) {
        for (int j = 2; (i * j) <= SZ(num); j++) {
            num[i * j] = false;
        }
    }
}
*/
using Graph = vector<vector<int>>;
typedef pair<int, int> P;
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};
//
//
//

//フィボナッチ数列
int fib(int num) {
    vector<int> dp(10000);  //計算結果を入れる
    dp[0] = 0;
    dp[1] = 1;
    for (int i = 2; i <= num; i++) {
        dp[i] = dp[i - 1] + dp[i - 2];
    }
    return dp[num];
}

// 桁和を計算する関数
int digit_sum(int n) {
    int ans = 0;
    while (n > 0) {
        ans += n % 10;
        n /= 10;
    }
    return ans;
}

main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    //
    int n, ac_cnt = 0, wa_cnt = 0, tle_cnt = 0, re_cnt = 0;
    string s;
    cin >> n;
    for(int i = 0; i < n; i++){
        cin >> s;
        if(s=="AC"){
            ac_cnt++;
        }
        if(s=="WA"){
            wa_cnt++;
        }
        if(s=="TLE"){
            tle_cnt++;
        }
        if(s=="RE"){
            re_cnt++;
        }
    }
    cout << "AC x " << ac_cnt << endl;
    cout << "WA x " << wa_cnt << endl;
    cout << "TLE x " << tle_cnt << endl;
    cout << "RE x " << re_cnt << endl;

    return 0;
}

Score : 200 points

Problem Statement

Takahashi is participating in a programming contest called AXC002, and he has just submitted his code to Problem A.

The problem has N test cases.

For each test case i (1\leq i \leq N), you are given a string S_i representing the verdict for that test case. Find the numbers of test cases for which the verdict is AC, WA, TLE, and RE, respectively.

See the Output section for the output format.

Constraints

1 \leq N \leq 10^5

S_i is AC, WA, TLE, or RE.

Input

Input is given from Standard Input in the following format:

N
S_1
\vdots
S_N

Output

Let C_0, C_1, C_2, and C_3 be the numbers of test cases for which the verdict is AC, WA, TLE, and RE, respectively. Print the following:

AC x C_0
WA x C_1
TLE x C_2
RE x C_3

Sample Input 1

6
AC
TLE
AC
AC
WA
TLE

Sample Output 1

AC x 3
WA x 1
TLE x 2
RE x 0

We have 3, 1, 2, and 0 test case(s) for which the verdict is AC, WA, TLE, and RE, respectively.

Sample Input 2

10
AC
AC
AC
AC
AC
AC
AC
AC
AC
AC

Sample Output 2

AC x 10
WA x 0
TLE x 0
RE x 0


#include <bits/stdc++.h>
#include <stdio.h>

using namespace std;
#define inputInt(a) int a; cin >> a;
#define inputInt2(a,b) int a; int b; cin >> a >> b;
#define inputInt3(a,b,c) int a; int b; int c; cin >> a >> b >> c;
#define inputLong(a) long a;cin >> a;
#define inputIntArray(a,N) int a[N];for(int i=0;i<N;i++){cin >> a[i];}
#define inputLongArray(a,N) long a[N];for(int i=0;i<N;i++){cin >> a[i];}
#define inputIntArray2(a,b,N) int a[N]; int b[N]; for(int i=0;i<N;i++){cin >> a[i] >> b[i];}
#define output(answer) cout << answer << endl;
#define fN(i,N)    for(int i=0; i<N; i++)
#define fSE(i,s,e) for(int i=s; i<=e; i++)
#define ll long long
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define FOR(i,N) for(int i=0; i<N; i++)
#define cinv(v,N) vector<int> v(N); for(int i=0; i<N; i++){ cin >> v[i]; }
#define mt make_tuple
#define vll vector<long long int>
#define vvll vector<vector<long long int>>
#define all(v) (v).begin(),(v).end()
#define show(v) FOR(i,v.size()){ cout << v[i] << " "; } cout << endl;
#define showshow(v) FOR(i,v.size()){ FOR(j,v[i].size()){ cout << v[i][j] << " "; } cout << endl; }
const ll MOD = 1e9+7;
const ll MOD2 = 998244353;
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; }

//Combination計算
const int LIMIT = 1e6;//1e6→50ms,1e7→500ms,1e8→5000ms程度。
//const ll MOD = 1e9+7;
vector<ll> FacMod(LIMIT), InvMod(LIMIT), FacInvMod(LIMIT);
const int dpComTableLIMIT = 3e3+1;
vector<vector<ll>> Com(dpComTableLIMIT,vector<ll>(dpComTableLIMIT,0));
const int ComKTableLIMIT = 1e7;
vector<ll> ComK(ComKTableLIMIT);

void DPComInit(){
  //Com[0][j]は0で初期化されている。
  for(int i=0; i<dpComTableLIMIT; i++){ Com[i][0] = 1; }

  for(int i=1; i<dpComTableLIMIT; i++){
    for(int j=1; j<dpComTableLIMIT; j++){
      Com[i][j] = (Com[i-1][j-1]+Com[i-1][j])%MOD;
    }
  }
}

void ComKInit(ll N){
  for(int i=0; i<=ComKTableLIMIT; i++){
    if(0<i && i<=N/2){
      ComK[i] = (ComK[i-1]*(N-i+1)/i)%MOD;
    }else if(i==0){
      ComK[i] = 1;
    }else if(N/2<i && i<=N){
      ComK[i] = ComK[N-i];
    }else{//i>Nの時
      ComK[i] = -1;
    }
  }
}

void ComInit(){//tableを作成
  FacMod[0] = FacMod[1] = 1;
  InvMod[0] = 0; InvMod[1] = 1; //InvMod[0]は定義できない
  FacInvMod[0] = FacInvMod[1] = 1;

  for(int i=2; i<LIMIT; i++){
    FacMod[i] = FacMod[i-1]*i%MOD;
    InvMod[i] = -InvMod[MOD%i]*(MOD/i)%MOD + MOD;
    FacInvMod[i] = FacInvMod[i-1] * InvMod[i]%MOD;
  }
}

ll ComMod(int n, int k){
  if(n>=0 && k>=0 && n>=k){
    return FacMod[n]*(FacInvMod[k]*FacInvMod[n-k]%MOD)%MOD;
  }else{
    return 0; //戻り値を0にする方が計算結果に影響が少ない。
  }
}

//繰り返し2乗法
ll power(ll x, ll y){//繰り返し2乗法
  if(y < 0){
    return 0;
  }else if(y == 0){
    return 1LL;
  }else if(y%2 == 0){//偶数
    return power(x,y/2)*power(x,y/2);
  }else{//奇数
    return power(x,y-1)*x;
  }
}

//繰り返し2乗法
ll mpower(ll x, ll y, ll m){//繰り返し2乗法
  if(y < 0){
    return 0;
  }else if(y == 0){
    return 1LL;
  }else if(y%2 == 0){//偶数
    return mpower(x,y/2,m)*mpower(x,y/2,m)%m;
  }else{//奇数
    return mpower(x,y-1,m)*x%m;
  }
}

//pair型のソート
bool compare_by_second(pair<ll,ll> a, pair<ll,ll> b){
  return a.second != b.second ? a.second < b.second : a.first < b.first;
}

class UnionFind {
  vector<int> parent;
  vector<int> rank;

  public:

  UnionFind(int N);
  int root(int x);
  void unite(int x, int y);
  bool isUnited(int x, int y);
  int getRank(int x);
};

UnionFind::UnionFind(int N):parent(N),rank(N,1){
  for(int i=0; i<N; i++){parent[i]=i;}
}

int UnionFind::root(int x){
  if(parent[x] == x) return x;
  return root(parent[x]);
  /*parent[x] = parent[parent[x]];
  if(parent[x]!=parent[parent[x]]) rank[x]-=1;*/
}

void UnionFind::unite(int x, int y) { // xとyの木を併合
  int rx = root(x); //xの根をrx
  int ry = root(y); //yの根をry
  if (rx == ry) return; //xとyの根が同じ(=同じ木にある)時はそのまま

  if(rank[x] >= rank[y]){parent[ry] = rx; rank[y]+=1;}//大きい木に小さい木が併合される
  else{parent[rx] = ry; rank[x]+=1;}//根に直接辺を張る
}

bool UnionFind::isUnited(int x, int y) { // 2つのデータx, yが属する木が同じならtrueを返す
  int rx = root(x);
  int ry = root(y);
  parent[x]=rx; parent[y]=ry;rank[x]=2;rank[y]=2;
  return rx == ry;
}

int UnionFind::getRank(int x){
  return rank[x];
}

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

  ll N; cin >> N;
  vector<string> s(N); FOR(i,N){ cin >> s[i]; }

  //main
  ll ac = 0;
  ll wa = 0;
  ll tle = 0;
  ll re = 0;

  FOR(i,N){
    if(s[i] == "AC"){
      ac ++;
    }else if(s[i] == "WA"){
      wa ++;
    }else if(s[i] == "TLE"){
      tle ++;
    }else if(s[i] == "RE"){
      re ++;
    }
  }

  // output
  cout << "AC x " << ac << endl;
  cout << "WA x " << wa << endl;
  cout << "TLE x " << tle << endl;
  cout << "RE x " << re << endl;

  return 0;
}


#include <iostream>
#include <string>

int main()
{
	int n;

	std::cin >> n;

	int ac = 0;
	int wa = 0;
	int tle = 0;
	int re = 0;

	std::string jg;
	for (size_t i = 0; i < n; i++)
	{
		std::cin >> jg;
		if (jg == "AC")
		{
			++ac;
		}
		else if (jg == "WA")
		{
			++wa;
		}
		else if (jg == "TLE")
		{
			++tle;
		}
		else if (jg == "RE")
		{
			++re;
		}
		else {}
	}

	std::cout << "AC x " << ac << std::endl;
	std::cout << "WA x " << wa << std::endl;
	std::cout << "TLE x " << tle << std::endl;
	std::cout << "RE x " << re << std::endl;

	return 0;
}


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

typedef long long ll;
typedef long double ld;

#define pb push_back
#define sort(s) sort(s.begin(),s.end())
#define reverse(s) reverse(s.begin(),s.end())
#define rep(i,n) for(ll (i)=0;(i)<(n);(i)++)

const ll mod = 1e9+7;

//最大公約数
ll gcd(ll a, ll b) {
    return b ? gcd(b, a%b) : a;
}

//最小公倍数
ll lcm(ll a, ll b) {
    return a / gcd(a, b) * b;
}

//素数判定
bool isPrime(ll x){
    ll i;
    if(x < 2)return 0;
    else if(x == 2) return 1;
    if(x%2 == 0) return 0;
    for(i = 3; i*i <= x; i += 2) if(x%i == 0) return 0;
    return 1;
}

//桁和
int digsum(ll n) {
    int res = 0;
    while(n > 0) {
        res += n%10;
        n /= 10;
    }
    return res;
}

//桁数
int dignum(ll n) {
    int res = 0;
    while(n > 0) {
        res++;
        n /= 10;
    }
    return res;
}

//文字列中の特定の文字カウント
ll stringcount(string s,char c){
    return count(s.cbegin(),s.cend(),c);
}

//階乗
ll ka(ll i){
    ll res=1;
    while(i>0){
        res=res*i;
        i--;
    }
    return res;
}

//ncr
ll ncr(ll x,ll y){
    ll a = x;
    ll b = 1;
    if(y>(x/2)){
        y=x-y;
    }
    for(ll i=1;i<y;i++){
        a*=x-i;
        b*=i+1;
    }
    return a/b;
}

//フィボナッチ数列
ll f(ll x){
    ll dp[x+1];
    dp[1]=1;
    dp[2]=1;
    for(ll i=3;i<=x;i++){
        dp[i]=dp[i-1]+dp[i-2];
    }
    return dp[x];
}

//char->int
int ctoi(char c){
    return c-'0';
}

vector<pair<long long, long long> > prime_factorize(long long N) {
    vector<pair<long long, long long> > res;
    for (long long a = 2; a * a <= N; ++a) {
        if (N % a != 0) continue;
        long long ex = 0; // 指数

        // 割れる限り割り続ける
        while (N % a == 0) {
            ++ex;
            N /= a;
        }

        // その結果を push
        res.push_back({a, ex});
    }

    // 最後に残った数について
    if (N != 1) res.push_back({N, 1});
    return res;
}

int main(){
    int n;
    cin >> n;
    string s[n];
    rep(i,n) cin >> s[i];

    map<string,int> mp;

    for(int i=0;i<n;i++){
        mp[s[i]]++;
    }

    cout << "AC x " << mp["AC"] << endl;
    cout << "WA x " << mp["WA"] << endl;
    cout << "TLE x " << mp["TLE"] << endl;
    cout << "RE x " << mp["RE"] << endl;
}


#include<bits/stdc++.h>
using namespace std;
#define int long long
#define rep(i,n) for(int i=0;i<n;i++)
#define REP(i,n) for(int i=1;i<n;i++)
#define rev(i,n) for(int i=n-1;i>=0;i--)
#define all(v) v.begin(),v.end()
#define P pair<int,int>
#define len(s) (int)s.size()

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 int cmp(vector<T>&v,T &a){
	return lower_bound(all(v),a)-v.begin();
}
constexpr int mod = 1e9+7;
constexpr int inf = 3e18;

int a[4];
signed main(){
	int N;
	cin>>N;
	while(N--){
		string s;cin>>s;
		if(s=="AC")a[0]++;
		else if(s=="WA")a[1]++;
		else if(s=="TLE")a[2]++;
		else a[3]++;
	}
	cout<<"AC x "<<a[0]<<endl;
	cout<<"WA x "<<a[1]<<endl;
	cout<<"TLE x "<<a[2]<<endl;
	cout<<"RE x "<<a[3]<<endl;
}


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

int main() {
 int h,w,k;
 int ans=0;
 cin >>h >>w >>k;
 vector<vector<char>> G(h);
 int black_count =0;

 rep(i,h){
     rep(j,w){
         char ch;
         cin>>ch;
         if(ch=='#')black_count++;
         G[i].push_back(ch);
     }
 }

for(int i=0;i<(1<<h);i++){
    for(int j=0;j<(1<<w);j++){
        vector<vector<char>> tmp=G;
        int cnt=0;
        for(int y=0;y<h;y++){
            if(i&(1<<y)){
                rep(l,w){
                   if(tmp[y][l]=='#'){
                       cnt++;
                       tmp[y][l]='.';
                   }
                }
            }
        }

        for(int x=0;x<w;x++){
            if(j&(1<<x)){
                rep(m,h){
                   if(tmp[m][x]=='#'){
                       cnt++;
                       tmp[m][x]='.';
                   }
                }
            }
        }
        if(black_count-cnt==k)ans++;
    }
}
cout<<ans<<endl;
}

Score : 300 points

Problem Statement

We have a grid of H rows and W columns of squares. The color of the square at the i-th row from the top and the j-th column from the left (1 \leq i \leq H, 1 \leq j \leq W) is given to you as a character c_{i,j}: the square is white if c_{i,j} is ., and black if c_{i,j} is #.

Consider doing the following operation:

Choose some number of rows (possibly zero), and some number of columns (possibly zero). Then, paint red all squares in the chosen rows and all squares in the chosen columns.

You are given a positive integer K. How many choices of rows and columns result in exactly K black squares remaining after the operation? Here, we consider two choices different when there is a row or column chosen in only one of those choices.

Constraints

1 \leq H, W \leq 6

1 \leq K \leq HW

c_{i,j} is . or #.

Input

Input is given from Standard Input in the following format:

H W K
c_{1,1}c_{1,2}...c_{1,W}
c_{2,1}c_{2,2}...c_{2,W}
:
c_{H,1}c_{H,2}...c_{H,W}

Output

Print an integer representing the number of choices of rows and columns satisfying the condition.

Sample Input 1

2 3 2
..#
###

Sample Output 1

5

Five choices below satisfy the condition.

The 1-st row and 1-st column

The 1-st row and 2-nd column

The 1-st row and 3-rd column

The 1-st and 2-nd column

The 3-rd column

Sample Input 2

2 3 4
..#
###

Sample Output 2

1

One choice, which is choosing nothing, satisfies the condition.

Sample Input 3

2 2 3
##
##

Sample Output 3

0

Sample Input 4

6 6 8
..##..
.#..#.
#....#
######
#....#
#....#

Sample Output 4

208


#include<bits/stdc++.h>
using namespace std;
typedef long long int ll;
#define PI 3.141592653589793238
int main()
{
    // #ifndef ONLINE_JUDGE
    // freopen("/home/pushkar/cpp/input.txt", "r", stdin);
    // freopen("/home/pushkar/cpp/output.txt", "w", stdout);
    // #endif
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    ll h, w, k, i, j, p, q, ans = 0, c;
    cin >> h >> w >> k;
    vector <string> mat(h);
    for (i=0;i<h;i++)
    {
        cin >> mat[i];
    }
    for (i=0;i<(1<<h);i++)
    {
        for (j=0;j<(1<<w);j++)
        {
            c = 0;
            for (p=0;p<h;p++)
            {
                for (q=0;q<w;q++)
                {
                    if ((1<<p)&i && (1<<q)&j && mat[p][q] == '#')
                    c++;
                }
            }
            if (c == k)
            ans++;
        }
    }
    cout << ans;
    return 0;
}


#include <algorithm>
#include <cmath>
#include <vector>
#include <functional>
#include <cstdlib>
#include <map>
#include <set>
#include <iostream>
#include <string>
#include <ctype.h>
#include <climits>
#include <queue>
using namespace std;
#define REP(i, n) for(int i = 0; i < (int)(n); i++)
typedef long long ll;

int main(){
	int h,w,k;
	cin >> h >> w >> k;
	vector<vector<char>> c(h,vector<char>(w));
	REP(i,h) REP(j,w) cin >> c[i][j];

	int ans=0;
	for(int bit=0; bit<(1<<(h+w)); ++bit){
		int a=0;
		REP(i,c.size()){
			if(bit & (1<<i)) continue;
			REP(j,c[i].size()){
				if(bit & (1<<(j+h))) continue;
				if(c[i][j]=='#') a++;
			}
		}

		if(a==k) ans++;
	}

	cout << ans <<endl;

	return 0;
}


#include<bits/stdc++.h>
#define all(v) v.begin(), v.end()
#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define all(v) v.begin(), v.end()

using namespace std;
typedef long long LL;
const LL MOD = 1e9 + 7;
const double PI = 2 * acos(0);

bool cmp(int x, int y) {
	return abs(x) < abs(y);
}

int main() {
	int n, k;
	scanf("%d%d", &n, &k);
	LL arr[n + 1];
	for (int i = 0; i < n; i++) {
		scanf("%lld", &arr[i]);
	}
	sort (arr, arr + n, cmp);
	stack<LL> pos, neg;
	if (arr[n - k] == 0) printf("0\n");
	else {
		for (int i = n - 1; i >= n - k; i--) {
			if (arr[i] > 0) pos.push(arr[i]);
			else neg.push(arr[i]);
		}
		int l1 = neg.size();
		if (l1 % 2) {
			LL x = INT_MAX;
			LL y = INT_MAX;
			for (int i = 0; i < n - k; i++) {
				if (arr[i] > 0) x = arr[i];
				else y = arr[i];
			}
			if (!pos.empty() && y != INT_MAX) {
				LL cnt1 = neg.top() * y;
				LL cnt2 = -1;
				if (x != INT_MAX) {
					cnt2 = x * pos.top();
				}
				if (cnt1 > cnt2) {
					pos.pop();
					LL ans = y;
					while (!pos.empty()) {
						ans *= pos.top();
						ans %= MOD;
						pos.pop();
					}
					while (!neg.empty()) {
						ans *= -neg.top();
						ans %= MOD;
						neg.pop();
					}
					printf("%lld\n", ans);
				} else {
					neg.pop();
					LL ans = x;
					while (!pos.empty()) {
						ans *= pos.top();
						ans %= MOD;
						pos.pop();
					}
					while (!neg.empty()) {
						ans *= -neg.top();
						ans %= MOD;
						neg.pop();
					}
					printf("%lld\n", ans);
				}
			} else if (x != INT_MAX) {
				neg.pop();
				LL ans = x;
				while (!pos.empty()) {
					ans *= pos.top();
					ans %= MOD;
					pos.pop();
				}
				while (!neg.empty()) {
					ans *= -neg.top();
					ans %= MOD;
					neg.pop();
				}
				printf("%lld\n", ans);
			} else {
				LL ans = 1;
				for (int i = 0; i < k; i++) {
					ans *= arr[i];
					while (ans < 0) ans += MOD;
					ans %= MOD;
				}
				printf("%lld\n", ans);
			}
		} else {
			LL ans = 1;
			while (!pos.empty()) {
				ans *= pos.top();
				ans %= MOD;
				pos.pop();
			}
			while (!neg.empty()) {
				ans *= -neg.top();
				ans %= MOD;
				neg.pop();
			}
			printf("%lld\n", ans);
		}
	}
}

Score : 500 points

Problem Statement

Given are N integers A_1,\ldots,A_N.

We will choose exactly K of these elements. Find the maximum possible product of the chosen elements.

Then, print the maximum product modulo (10^9+7), using an integer between 0 and 10^9+6 (inclusive).

Constraints

1 \leq K \leq N \leq 2\times 10^5

|A_i| \leq 10^9

Input

Input is given from Standard Input in the following format:

N K
A_1 \ldots A_N

Output

Print the maximum product modulo (10^9+7), using an integer between 0 and 10^9+6 (inclusive).

Sample Input 1

4 2
1 2 -3 -4

Sample Output 1

12

The possible products of the two chosen elements are 2, -3, -4, -6, -8, and 12, so the maximum product is 12.

Sample Input 2

4 3
-1 -2 -3 -4

Sample Output 2

1000000001

The possible products of the three chosen elements are -24, -12, -8, and -6, so the maximum product is -6.

We print this value modulo (10^9+7), that is, 1000000001.

Sample Input 3

2 1
-1 1000000000

Sample Output 3

1000000000

The possible products of the one chosen element are -1 and 1000000000, so the maximum product is 1000000000.

Sample Input 4

10 10
1000000000 100000000 10000000 1000000 100000 10000 1000 100 10 1

Sample Output 4

999983200

Be sure to print the product modulo (10^9+7).