Score : <var>700</var> points

Problem Statement

In the State of Takahashi in AtCoderian Federation, there are <var>N</var> cities, numbered <var>1, 2, ..., N</var>.
<var>M</var> bidirectional roads connect these cities.
The <var>i</var>-th road connects City <var>A_i</var> and City <var>B_i</var>.
Every road connects two distinct cities.
Also, for any two cities, there is at most one road that directly connects them.

One day, it was decided that the State of Takahashi would be divided into two states, Taka and Hashi.
After the division, each city in Takahashi would belong to either Taka or Hashi.
It is acceptable for all the cities to belong Taka, or for all the cities to belong Hashi.
Here, the following condition should be satisfied:

Any two cities in the same state, Taka or Hashi, are directly connected by a road.

Find the minimum possible number of roads whose endpoint cities belong to the same state.
If it is impossible to divide the cities into Taka and Hashi so that the condition is satisfied, print -1.

Constraints

<var>2 \leq N \leq 700</var>

<var>0 \leq M \leq N(N-1)/2</var>

<var>1 \leq A_i \leq N</var>

<var>1 \leq B_i \leq N</var>

<var>A_i \neq B_i</var>

If <var>i \neq j</var>, at least one of the following holds: <var>A_i \neq A_j</var> and <var>B_i \neq B_j</var>.

If <var>i \neq j</var>, at least one of the following holds: <var>A_i \neq B_j</var> and <var>B_i \neq A_j</var>.

Input

Input is given from Standard Input in the following format:

<var>N</var> <var>M</var>
<var>A_1</var> <var>B_1</var>
<var>A_2</var> <var>B_2</var>
<var>:</var>
<var>A_M</var> <var>B_M</var>

Output

Print the answer.

Sample Input 1

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

Sample Output 1

4

For example, if the cities <var>1, 2</var> belong to Taka and the cities <var>3, 4, 5</var> belong to Hashi, the condition is satisfied.
Here, the number of roads whose endpoint cities belong to the same state, is <var>4</var>.

Sample Input 2

5 1
1 2

Sample Output 2

-1

In this sample, the condition cannot be satisfied regardless of which cities belong to each state.

Sample Input 3

4 3
1 2
1 3
2 3

Sample Output 3

3

Sample Input 4

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

Sample Output 4

21

int solution() {
  int i;
  int u;
  int w;
  int N;
  int M;
  int adj[701][701] = {};
  scanf("%d %d", &N, &M);
  for (i = 1; i <= M; i++) {
    scanf("%d %d", &u, &w);
    adj[u][w] = 1;
    adj[w][u] = 1;
  }

  int k;
  int flag[701] = {};
  int size[701];
  int q[701];
  int head;
  int tail;
  for (i = 1, k = 0; i <= N; i++) {
    if (flag[i] != 0) {
      continue;
    }
    size[++k] = 1;
    flag[i] = 1;
    q[0] = i;
    for (head = 0, tail = 1; head < tail; head++) {
      for (u = q[head], w = 1; w <= N; w++) {
        if (w != u && adj[u][w] == 0) {
          if (flag[w] == 0) {
            flag[w] = -flag[u];
            size[k] += flag[w];
            q[tail++] = w;
          } else if (flag[w] == flag[u]) {
            break;
          }
        }
      }
      if (w <= N) {
        break;
      }
    }
    if (head < tail) {
      break;
    }
    size[k] = abs(size[k]);
  }
  if (i <= N) {
    printf("-1\n");
    fflush(stdout);
    return 0;
  }

  int j;
  int dp[701][1401] = {};
  for (i = 1, dp[0][N] = 1; i <= k; i++) {
    for (j = size[i]; j <= N * 2 - size[i]; j++) {
      dp[i][j + size[i]] |= dp[i - 1][j];
      dp[i][j - size[i]] |= dp[i - 1][j];
    }
  }

  for (i = N; i >= 0; i--) {
    if (dp[k][i] == 1) {
      break;
    }
  }
  for (j = N; j <= N * 2; j++) {
    if (dp[k][j] == 1) {
      break;
    }
  }
  j = (N - i > j - N) ? j - N : N - i;
  printf("%d\n", ((N + j) * (N + j - 2) + (N - j) * (N - j - 2)) / 8);
  fflush(stdout);
  return 0;
}

fn solution() {
    input! {
        n: usize,
        m: usize,
        es: [(usize1, usize1); m]
    }
    let mut g = vec![(0..n).collect::<BTreeSet<usize>>(); n];
    for i in 0..n {
        g[i].remove(&i);
    }
    for &(a, b) in &es {
        g[a].remove(&b);
        g[b].remove(&a);
    }
    let g = g
        .iter()
        .map(|s| s.iter().cloned().collect::<Vec<usize>>())
        .collect::<Vec<_>>();
    let mut done = vec![None; n];
    let mut ds = vec![];
    for i in 0..n {
        if done[i].is_some() {
            continue;
        }
        let mut ct: i64 = 1;
        let mut cf: i64 = 0;
        done[i] = Some(true);
        let mut st = vec![i];
        while let Some(v) = st.pop() {
            let f = !done[v].unwrap();
            for &c in &g[v] {
                if let Some(fc) = done[c] {
                    if fc ^ f {
                        println!("-1");
                        return;
                    }
                    continue;
                }
                if f {
                    ct += 1;
                } else {
                    cf += 1;
                }
                done[c] = Some(f);
                st.push(c);
            }
        }
        ds.push((ct - cf).unsigned_abs() as usize);
    }
    let mut dp = vec![false; n + 1];
    dp[ds[0]] = true;
    for i in 1..ds.len() {
        let mut next = vec![false; n + 1];
        for j in 0..n + 1 - ds[i] {
            next[j + ds[i]] |= dp[j];
        }
        for j in 0..n + 1 {
            next[(j as i64 - ds[i] as i64).unsigned_abs() as usize] |= dp[j];
        }
        dp = next;
    }
    for i in 0..n + 1 {
        if dp[i] {
            println!(
                "{}",
                ((n + i) / 2) * ((n + i) / 2 - 1) / 2 + ((n - i) / 2) * ((n - i) / 2 - 1) / 2
            );
            return;
        }
    }
}


Score : <var>400</var> points

Problem Statement

We have <var>N</var> integers. The <var>i</var>-th integer is <var>A_i</var>.

Find <var>\sum_{i=1}^{N-1}\sum_{j=i+1}^{N} (A_i \mbox{ XOR } A_j)</var>, modulo <var>(10^9+7)</var>.

What is <var>\mbox{ XOR }</var>?

The XOR of integers <var>A</var> and <var>B</var>, <var>A \mbox{ XOR } B</var>, is defined as follows:

When <var>A \mbox{ XOR } B</var> is written in base two, the digit in the <var>2^k</var>'s place (<var>k \geq 0</var>) is <var>1</var> if either <var>A</var> or <var>B</var>, but not both, has <var>1</var> in the <var>2^k</var>'s place, and <var>0</var> otherwise.

For example, <var>3 \mbox{ XOR } 5 = 6</var>. (In base two: <var>011 \mbox{ XOR } 101 = 110</var>.)

Constraints

<var>2 \leq N \leq 3 \times 10^5</var>

<var>0 \leq A_i < 2^{60}</var>

All values in input are integers.

Input

Input is given from Standard Input in the following format:

<var>N</var>
<var>A_1</var> <var>A_2</var> <var>...</var> <var>A_N</var>

Output

Print the value <var>\sum_{i=1}^{N-1}\sum_{j=i+1}^{N} (A_i \mbox{ XOR } A_j)</var>, modulo <var>(10^9+7)</var>.

Sample Input 1

3
1 2 3

Sample Output 1

6

We have <var>(1\mbox{ XOR } 2)+(1\mbox{ XOR } 3)+(2\mbox{ XOR } 3)=3+2+1=6</var>.

Sample Input 2

10
3 1 4 1 5 9 2 6 5 3

Sample Output 2

237

Sample Input 3

10
3 14 159 2653 58979 323846 2643383 27950288 419716939 9375105820

Sample Output 3

103715602

Print the sum modulo <var>(10^9+7)</var>.

int solution() {
  int n;
  scanf("%d", &n);
  int i;
  int j;
  long long int a[300005];
  for (i = 0; i < n; i++) {
    scanf("%lld", &a[i]);
  }
  int b[300005][62];
  for (i = 0; i < n; i++) {
    for (j = 0; j < 62; j++) {
      b[i][j] = (int)(a[i] % 2);
      a[i] /= 2;
    }
  }
  long long int ans = 0;
  long long int c0;
  long long int c1;
  long long int q = 1;
  long long int p = 1000000007;
  for (j = 0; j < 62; j++) {
    c0 = c1 = 0;
    for (i = 0; i < n; i++) {
      if (b[i][j] == 0) {
        c0++;
      } else {
        c1++;
      }
    }
    ans = (ans + q * c0 % p * c1 % p) % p;
    q = q * 2 % p;
  }
  printf("%lld\n", ans);
  return 0;
}

fn solution() {
    let mut input_str = String::new();
    std::io::stdin().read_to_string(&mut input_str).unwrap();

    let input_parts = input_str.split_whitespace().collect::<Vec<_>>();
    let mut input_parts_it = input_parts.iter().cloned();
    let mut next = || input_parts_it.next().unwrap();

    let n: usize = next().parse().unwrap();
    let a: Vec<u64> = (0..n).map(|_| next().parse().unwrap()).collect();

    let mut a_sum = vec![0; a.len() + 1];

    let sum = (0..60u32)
        .map(|bit| {
            for i in 1..a.len() + 1 {
                a_sum[i] = a_sum[i - 1] + ((a[i - 1] >> bit) & 1);
            }

            let sum: u64 = (0..a.len() - 1)
                .map(|i| {
                    let j = i + 1..a.len();
                    let set_count = a_sum[j.end] - a_sum[j.start];
                    if (a[i] >> bit) & 1 != 0 {
                        (j.end - j.start) as u64 - set_count
                    } else {
                        set_count
                    }
                })
                .sum();

            (0..bit).fold(sum, |x, _| (x * 2) % 1000000007)
        })
        .fold(0, |x, y| (x + y) % 1000000007);

    println!("{}", sum);
}


Score : <var>100</var> points

Problem Statement

It is known that the area of a regular dodecagon inscribed in a circle of radius <var>a</var> is <var>3a^2</var>.

Given an integer <var>r</var>, find the area of a regular dodecagon inscribed in a circle of radius <var>r</var>.

Constraints

<var>1 \leq r \leq 100</var>

<var>r</var> is an integer.

Input

Input is given from Standard Input in the following format:

<var>r</var>

Output

Print an integer representing the area of the regular dodecagon.

Sample Input 1

4

Sample Output 1

48

The area of the regular dodecagon is <var>3 \times 4^2 = 48</var>.

Sample Input 2

15

Sample Output 2

675

Sample Input 3

80

Sample Output 3

19200

int solution(void) {

  int r = 0;
  scanf("%d", &r);

  printf("%d", 3 * r * r);

  return 0;
}

fn solution() {
    let mut buf = String::new();
    io::stdin().read_line(&mut buf).expect("");
    let r = buf.trim().parse::<i32>().ok().unwrap();
    println!("{}", 3 * r * r);
}


Shaass has decided to hunt some birds. There are n horizontal electricity wires aligned parallel to each other. Wires are numbered 1 to n from top to bottom. On each wire there are some oskols sitting next to each other. Oskol is the name of a delicious kind of birds in Shaass's territory. Supposed there are ai oskols sitting on the i-th wire.  Sometimes Shaass shots one of the birds and the bird dies (suppose that this bird sat at the i-th wire). Consequently all the birds on the i-th wire to the left of the dead bird get scared and jump up on the wire number i - 1, if there exists no upper wire they fly away. Also all the birds to the right of the dead bird jump down on wire number i + 1, if there exists no such wire they fly away. Shaass has shot m birds. You're given the initial number of birds on each wire, tell him how many birds are sitting on each wire after the shots.

int solution(void) {
  int n = 0;
  int m = 0;
  int x = 0;
  int y = 0;
  scanf("%d", &n);
  int arr[n];
  for (int i = 0; i < n; i++) {
    scanf("%d", &arr[i]);
  }
  scanf("%d", &m);
  for (int i = 0; i < m; i++) {
    scanf("%d", &x);
    scanf("%d", &y);
    if (x - 1 != 0) {
      arr[x - 2] = y - 1 + arr[x - 2];
    }
    if (x != n) {
      arr[x] = arr[x - 1] - y + arr[x];
    }
    arr[x - 1] = 0;
  }
  for (int i = 0; i < n; i++) {
    printf("%d\n", arr[i]);
  }
  return 0;
}

fn solution() {
    use std::io;
    let mut buf = String::new();
    io::stdin().read_line(&mut buf).unwrap();
    buf.clear();
    io::stdin().read_line(&mut buf).unwrap();
    let mut wires: Vec<u32> = buf
        .split(" ")
        .map(|str| str.trim().parse().unwrap())
        .collect();
    buf.clear();
    io::stdin().read_line(&mut buf).unwrap();
    let m = buf.trim().parse::<u32>().unwrap();
    for _ in 0..m {
        buf.clear();
        io::stdin().read_line(&mut buf).unwrap();
        let mut ns = buf.split(" ").map(|str| str.trim().parse::<u32>().unwrap());
        let x = ns.next().unwrap();
        let y = ns.next().unwrap();
        if x != 1 {
            wires[x as usize - 2] += y - 1;
        }
        if x != wires.len() as u32 {
            wires[x as usize] += wires[x as usize - 1] - y;
        }
        wires[x as usize - 1] = 0;
    }
    for wire in wires {
        println!("{}", wire);
    }
}


Vasya owns a cornfield which can be defined with two integers $$$n$$$ and $$$d$$$. The cornfield can be represented as rectangle with vertices having Cartesian coordinates $$$(0, d), (d, 0), (n, n - d)$$$ and $$$(n - d, n)$$$.    An example of a cornfield with $$$n = 7$$$ and $$$d = 2$$$. Vasya also knows that there are $$$m$$$ grasshoppers near the field (maybe even inside it). The $$$i$$$-th grasshopper is at the point $$$(x_i, y_i)$$$. Vasya does not like when grasshoppers eat his corn, so for each grasshopper he wants to know whether its position is inside the cornfield (including the border) or outside.Help Vasya! For each grasshopper determine if it is inside the field (including the border).

fn solution() {
    let mut buffer = String::new();
    let stdin = io::stdin();
    let mut handle = stdin.lock();

    handle.read_to_string(&mut buffer).unwrap();

    let arr = buffer
        .split_whitespace()
        .map(|x| x.parse::<i32>().unwrap())
        .collect::<Vec<_>>();
    let n = arr[0];
    let d = arr[1];
    let m = arr[2] as usize;
    for i in 0..m {
        let x = arr[3 + i * 2];
        let y = arr[3 + i * 2 + 1];

        let y1 = x + d;
        let y2 = x - d;
        let y3 = -x + d;
        let y4 = -x + 2 * n - d;

        if y <= y1 && y >= y2 && y >= y3 && y <= y4 {
            println!("YES");
        } else {
            println!("NO");
        }
    }
}


Baby Ehab is known for his love for a certain operation. He has an array $$$a$$$ of length $$$n$$$, and he decided to keep doing the following operation on it:   he picks $$$2$$$ adjacent elements; he then removes them and places a single integer in their place: their bitwise XOR. Note that the length of the array decreases by one. Now he asks you if he can make all elements of the array equal. Since babies like to make your life harder, he requires that you leave at least $$$2$$$ elements remaining.

int solution() {
  int a[2010];
  int t;
  scanf("%d", &t);
  while (t--) {
    int n;
    scanf("%d", &n);

    int temp = 0;
    for (int i = 1; i <= n; i++) {
      scanf("%d", &a[i]);
      temp ^= a[i];
    }
    if (temp == 0) {
      printf("YES\n");
      continue;
    }
    int cnt = 0;
    int tt = 0;
    for (int i = 1; i <= n; i++) {
      tt ^= a[i];
      if (tt == temp) {
        cnt++;
        tt = 0;
      }
    }
    if (cnt != 1 && cnt % 2 == 1) {
      printf("YES\n");
    } else {
      printf("NO\n");
    }
  }
  return 0;
}

fn solution() {
    use std::io::Read;
    let mut buf = String::new();
    std::io::stdin().read_to_string(&mut buf).unwrap();
    let mut itr = buf.split_whitespace();

    use std::io::Write;
    let out = std::io::stdout();
    let mut out = std::io::BufWriter::new(out.lock());

    let t: usize = scan!(usize);
    for _ in 1..=t {
        let n = scan!(usize);
        let a = scan!([u32; n]);

        let tot_xor = a.iter().fold(0, |x, a| x ^ *a);
        let mut cur = 0;
        let mut flag = tot_xor == 0;
        'done: for i in 0..n {
            cur ^= a[i];

            let mut lxor = 0;
            for j in i + 1..n {
                lxor ^= a[j];
                if cur == lxor ^ tot_xor ^ cur && cur == lxor {
                    flag = true;
                    break 'done;
                }
            }
        }
        if flag {
            print!("YES");
        } else {
            print!("NO");
        }
        print!("\n");
    }
}


Score : <var>400</var> points

Problem Statement

We have <var>N</var> points in a two-dimensional plane.

The coordinates of the <var>i</var>-th point <var>(1 \leq i \leq N)</var> are <var>(x_i,y_i)</var>.

Let us consider a rectangle whose sides are parallel to the coordinate axes that contains <var>K</var> or more of the <var>N</var> points in its interior.

Here, points on the sides of the rectangle are considered to be in the interior.

Find the minimum possible area of such a rectangle.

Constraints

<var>2 \leq K \leq N \leq 50</var>

<var>-10^9 \leq x_i,y_i \leq 10^9 (1 \leq i \leq N)</var>

<var>x_i≠x_j (1 \leq i<j \leq N)</var>

<var>y_i≠y_j (1 \leq i<j \leq N)</var>

All input values are integers. (Added at 21:50 JST)

Input

Input is given from Standard Input in the following format:

<var>N</var> <var>K</var>
<var>x_1</var> <var>y_1</var>
<var>:</var>
<var>x_{N}</var> <var>y_{N}</var>

Output

Print the minimum possible area of a rectangle that satisfies the condition.

Sample Input 1

4 4
1 4
3 3
6 2
8 1

Sample Output 1

21

One rectangle that satisfies the condition with the minimum possible area has the following vertices: <var>(1,1)</var>, <var>(8,1)</var>, <var>(1,4)</var> and <var>(8,4)</var>.

Its area is <var>(8-1) × (4-1) = 21</var>.

Sample Input 2

4 2
0 0
1 1
2 2
3 3

Sample Output 2

1

Sample Input 3

4 3
-1000000000 -1000000000
1000000000 1000000000
-999999999 999999999
999999999 -999999999

Sample Output 3

3999999996000000001

Watch out for integer overflows.

fn solution() {
    let mut s: String = String::new();
    std::io::stdin().read_to_string(&mut s).ok();
    let mut itr = s.split_whitespace();
    let n: usize = itr.next().unwrap().parse().unwrap();
    let K: usize = itr.next().unwrap().parse().unwrap();
    let xy: Vec<(i64, i64)> = (0..n)
        .map(|_| {
            (
                itr.next().unwrap().parse().unwrap(),
                itr.next().unwrap().parse().unwrap(),
            )
        })
        .collect();

    let mut ans = 1i64 << 62;
    for i in 0..n {
        for j in 0..n {
            for k in 0..n {
                for l in 0..n {
                    let R = max(xy[i].0, max(xy[j].0, max(xy[k].0, xy[l].0)));
                    let L = min(xy[i].0, min(xy[j].0, min(xy[k].0, xy[l].0)));
                    let U = max(xy[i].1, max(xy[j].1, max(xy[k].1, xy[l].1)));
                    let D = min(xy[i].1, min(xy[j].1, min(xy[k].1, xy[l].1)));
                    let mut cnt = 0;

                    for a in 0..n {
                        if L <= xy[a].0 && xy[a].0 <= R && D <= xy[a].1 && xy[a].1 <= U {
                            cnt += 1;
                        }
                    }
                    if cnt >= K {
                        ans = min(ans, (R - L) * (U - D));
                    }
                }
            }
        }
    }
    println!("{}", ans);
}


Little Vasya loves orange juice very much. That's why any food and drink in his kitchen necessarily contains orange juice. There are n drinks in his fridge, the volume fraction of orange juice in the i-th drink equals pi percent.One day Vasya decided to make himself an orange cocktail. He took equal proportions of each of the n drinks and mixed them. Then he wondered, how much orange juice the cocktail has.Find the volume fraction of orange juice in the final drink.

int solution() {
  float n;
  scanf("%f", &n);
  float a[100];
  float sum = 0;
  for (int i = 0; i < n; i++) {
    scanf("%f", &a[i]);
    sum = sum + a[i];
  }
  float orange = (sum / n) * 1.0;
  printf("%f", orange);
  return 0;
}

fn solution() {
    let n: f32 = {
        let mut buf = String::new();
        std::io::stdin().read_line(&mut buf).unwrap();
        buf.trim().parse().unwrap()
    };

    let mut input = String::new();
    std::io::stdin().read_line(&mut input).unwrap();
    println!(
        "{}",
        input
            .split_whitespace()
            .map(|elem| elem.parse::<f32>().unwrap())
            .sum::<f32>()
            / n
    )
}


ZS the Coder and Chris the Baboon are travelling to Udayland! To get there, they have to get on the special IOI bus. The IOI bus has n rows of seats. There are 4 seats in each row, and the seats are separated into pairs by a walkway. When ZS and Chris came, some places in the bus was already occupied.ZS and Chris are good friends. They insist to get a pair of neighbouring empty seats. Two seats are considered neighbouring if they are in the same row and in the same pair. Given the configuration of the bus, can you help ZS and Chris determine where they should sit?

fn solution() {
    let n: usize = {
        let mut buf = String::new();
        std::io::stdin().read_line(&mut buf).unwrap();
        buf.trim().parse().unwrap()
    };
    let mut bus = Vec::with_capacity(n);
    let mut found = false;

    for _ in 0..n {
        let mut row = String::new();
        std::io::stdin().read_line(&mut row).unwrap();
        if !found {
            let seats: Vec<&str> = row.trim().split('|').collect();
            if seats[0] == "OO" {
                bus.push(format!("++|{}", seats[1]));
                found = true;
            } else if seats[1] == "OO" {
                bus.push(format!("{}|++", seats[0]));
                found = true;
            } else {
                bus.push(row);
            }
        } else {
            bus.push(row);
        }
    }

    if found {
        println!("YES");
        for row in bus {
            println!("{}", row);
        }
    } else {
        println!("NO");
    }
}


You are given $$$n$$$ points with integer coordinates on a coordinate axis $$$OX$$$. The coordinate of the $$$i$$$-th point is $$$x_i$$$. All points' coordinates are distinct and given in strictly increasing order.For each point $$$i$$$, you can do the following operation no more than once: take this point and move it by $$$1$$$ to the left or to the right (i..e., you can change its coordinate $$$x_i$$$ to $$$x_i - 1$$$ or to $$$x_i + 1$$$). In other words, for each point, you choose (separately) its new coordinate. For the $$$i$$$-th point, it can be either $$$x_i - 1$$$, $$$x_i$$$ or $$$x_i + 1$$$.Your task is to determine if you can move some points as described above in such a way that the new set of points forms a consecutive segment of integers, i. e. for some integer $$$l$$$ the coordinates of points should be equal to $$$l, l + 1, \ldots, l + n - 1$$$.Note that the resulting points should have distinct coordinates.You have to answer $$$t$$$ independent test cases.

int solution() {

  int t;
  scanf("%d", &t);

  while (t--) {
    int n;
    int pass = 0;
    int f = 1;
    scanf("%d", &n);

    int v[n];

    for (int i = 0; i < n; ++i) {
      scanf("%d", (v + i));
    }

    for (int i = 1; i < n; ++i) {
      if (v[i] - 1 == v[i - 1]) {
        continue;
      }
      if (v[i] - 3 == v[i - 1] && !pass) {
        v[i]--;
        pass = 1;
      }

      else if (v[i] - 2 == v[i - 1] && !pass)
        pass = 1;

      else if (v[i] - 2 == v[i - 1] && pass)
        v[i]--;

      else {
        f = 0;
        break;
      }
    }

    printf("%s\n", (f ? "YES" : "NO"));
  }
}

fn solution() {
    let stdin = io::stdin();
    let mut buf = String::new();
    stdin.read_line(&mut buf).unwrap();
    let t: usize = buf.trim().parse().unwrap();

    for _ in 0..t {
        buf.clear();
        stdin.read_line(&mut buf).unwrap();
        buf.clear();
        stdin.read_line(&mut buf).unwrap();
        let a: Vec<i32> = buf.trim().split(" ").map(|x| x.parse().unwrap()).collect();

        let n = a.len();
        let x: i32 = a[n - 1] - a[0] - n as i32;

        if x <= 1 {
            println!("YES");
        } else {
            println!("NO");
        }
    }
}


Score : <var>100</var> points

Problem Statement

There are <var>S</var> sheep and <var>W</var> wolves.

If the number of wolves is greater than or equal to that of sheep, the wolves will attack the sheep.

If the wolves will attack the sheep, print unsafe; otherwise, print safe.

Constraints

<var>1 \leq S \leq 100</var>

<var>1 \leq W \leq 100</var>

Input

Input is given from Standard Input in the following format:

<var>S</var> <var>W</var>

Output

If the wolves will attack the sheep, print unsafe; otherwise, print safe.

Sample Input 1

4 5

Sample Output 1

unsafe

There are four sheep and five wolves. The number of wolves is not less than that of sheep, so they will attack them.

Sample Input 2

100 2

Sample Output 2

safe

Many a sheep drive away two wolves.

Sample Input 3

10 10

Sample Output 3

unsafe

int solution() {
  int s = 0;
  int w = 0;
  scanf("%d %d", &s, &w);

  if (s > w) {
    printf("safe\n");
  } else {
    printf("unsafe\n");
  }
}

fn solution() {
    let mut s = String::new();
    std::io::stdin().read_line(&mut s).ok();
    let s: Vec<i32> = s
        .trim()
        .split(' ')
        .map(|x| x.parse::<i32>().unwrap())
        .collect();

    if s[0] > s[1] {
        println!("safe");
    } else {
        println!("unsafe");
    }
}


Monocarp has been collecting rare magazines for quite a while, and now he has decided to sell them. He distributed the magazines between $$$n$$$ boxes, arranged in a row. The $$$i$$$-th box contains $$$a_i$$$ magazines. Some of the boxes are covered with lids, others are not. Suddenly it started to rain, and now Monocarp has to save as many magazines from the rain as possible. To do this, he can move the lids between boxes as follows: if the $$$i$$$-th box was covered with a lid initially, he can either move the lid from the $$$i$$$-th box to the box $$$(i-1)$$$ (if it exists), or keep the lid on the $$$i$$$-th box. You may assume that Monocarp can move the lids instantly at the same moment, and no lid can be moved more than once. If a box will be covered with a lid after Monocarp moves the lids, the magazines in it will be safe from the rain; otherwise they will soak.You have to calculate the maximum number of magazines Monocarp can save from the rain.

int solution(void) {
  int t;
  scanf("%d", &t);
  while (t--) {
    int n;
    int sum = 0;
    scanf("%d", &n);
    char s[n + 1];
    scanf("%s", s);
    int arr[n];
    for (int i = 0; i < n; i++) {
      scanf("%i", &arr[i]);
      if (s[i] == '1') {
        sum += arr[i];
      }
    }
    int var = -1;
    int small = -1;
    for (int i = 0; i < n; i++) {
      if (s[i] == '0' && s[i + 1] == '1') {
        var = arr[i];
        small = arr[i + 1];
      }
      if (s[i] == '1' && small != -1) {
        if (arr[i] < small) {
          small = arr[i];
        }
      }
      if (i == n - 1 && s[i] == '1') {
        if (var > small && var != -1 && small != -1) {
          sum -= small;
          sum += var;
        }
      }
      if (s[i] == '1' && s[i + 1] == '0' && var != -1) {
        if (var > small && var != -1 && small != -1) {
          sum -= small;
          sum += var;
        }
      }
    }

    printf("%i\n", sum);
  }
}

fn solution() {
    use std::io::*;
    let mut s = String::new();
    stdin().read_line(&mut s).ok();
    for _ in 0i32..(s.trim().parse().unwrap()) {
        stdin().read_line(&mut "".to_owned()).ok();
        let mut s = String::new();
        stdin().read_line(&mut s).ok();
        let lids: Vec<bool> = s.trim().chars().map(|x| x == '1').collect();
        let mut s = String::new();
        stdin().read_line(&mut s).ok();
        let books: Vec<i32> = s
            .split_ascii_whitespace()
            .map(|x| x.parse().unwrap())
            .collect();
        let ans = lids
            .into_iter()
            .zip(books.into_iter())
            .rev()
            .collect::<Vec<(bool, i32)>>();
        println!(
            "{}",
            ans.split_inclusive(|(i, _)| !*i)
                .map(|v| {
                    if v.iter().all(|(i, _)| *i) {
                        v.iter().map(|(_, x)| *x).sum::<i32>()
                    } else {
                        v.iter().map(|(_, x)| *x).sum::<i32>()
                            - v.iter().map(|(_, x)| *x).min().unwrap_or(0)
                    }
                })
                .sum::<i32>()
        )
    }
}


New Year is coming! Vasya has prepared a New Year's verse and wants to recite it in front of Santa Claus.Vasya's verse contains $$$n$$$ parts. It takes $$$a_i$$$ seconds to recite the $$$i$$$-th part. Vasya can't change the order of parts in the verse: firstly he recites the part which takes $$$a_1$$$ seconds, secondly — the part which takes $$$a_2$$$ seconds, and so on. After reciting the verse, Vasya will get the number of presents equal to the number of parts he fully recited.Vasya can skip at most one part of the verse while reciting it (if he skips more than one part, then Santa will definitely notice it).Santa will listen to Vasya's verse for no more than $$$s$$$ seconds. For example, if $$$s = 10$$$, $$$a = [100, 9, 1, 1]$$$, and Vasya skips the first part of verse, then he gets two presents.Note that it is possible to recite the whole verse (if there is enough time). Determine which part Vasya needs to skip to obtain the maximum possible number of gifts. If Vasya shouldn't skip anything, print 0. If there are multiple answers, print any of them.You have to process $$$t$$$ test cases.

int solution() {
  int32_t t;
  scanf("%d", &t);
  while (t--) {
    int32_t ch = 0;
    int32_t n;
    int32_t s;
    int32_t max = 0;
    int32_t sum = 0;
    int32_t ans = 0;
    scanf("%d%d", &n, &s);
    int32_t num[n];
    for (int i = 0; i < n; i++) {
      scanf("%d", &num[i]);
    }
    for (int i = 0; i < n; i++) {
      if (!ch && num[i] > max) {
        max = num[i];
        ans = i + 1;
      }
      if (sum + num[i] <= s) {
        sum += num[i];
      } else {
        if (!ch) {
          ch = 1;
          sum -= max;
        } else {
          break;
        }
      }
    }
    if (ch) {
      printf("%d\n", ans);
    } else {
      printf("0\n");
    }
  }
}

fn solution() {
    let stdin = io::stdin();
    let mut input = stdin.lock();
    let mut line = String::new();
    input.read_line(&mut line).unwrap();
    let mut queries: i8 = line.trim().parse().unwrap();
    while queries > 0 {
        line.clear();
        input.read_line(&mut line).unwrap();
        let mut numbers = line.split_whitespace();
        let blocks: i32 = numbers.next().unwrap().parse().unwrap();
        let max_time: i32 = numbers.next().unwrap().parse().unwrap();
        line.clear();
        input.read_line(&mut line).unwrap();
        let mut numbers = line.split_whitespace();
        let mut max_block: i32 = 0;
        let mut max_index: i32 = 0;
        let mut time: i32 = 0;
        let mut index: i32 = 1;
        loop {
            let block: i32 = numbers.next().unwrap().parse().unwrap();
            if block > max_block {
                max_block = block;
                max_index = index;
            }
            time += block;
            if time > max_time {
                break;
            }
            if index == blocks {
                max_index = 0;
                break;
            }
            index += 1;
        }
        println!("{}", max_index);
        queries -= 1;
    }
}


Score : <var>600</var> points

Problem Statement

Snuke stands on a number line. He has <var>L</var> ears, and he will walk along the line continuously under the following conditions:

He never visits a point with coordinate less than <var>0</var>, or a point with coordinate greater than <var>L</var>.

He starts walking at a point with integer coordinate, and also finishes walking at a point with integer coordinate.

He only changes direction at a point with integer coordinate.

Each time when Snuke passes a point with coordinate <var>i-0.5</var>, where <var>i</var> is an integer, he put a stone in his <var>i</var>-th ear.

After Snuke finishes walking, Ringo will repeat the following operations in some order so that, for each <var>i</var>, Snuke's <var>i</var>-th ear contains <var>A_i</var> stones:

Put a stone in one of Snuke's ears.

Remove a stone from one of Snuke's ears.

Find the minimum number of operations required when Ringo can freely decide how Snuke walks.

Constraints

<var>1 \leq L \leq 2\times 10^5</var>

<var>0 \leq A_i \leq 10^9(1\leq i\leq L)</var>

All values in input are integers.

Input

Input is given from Standard Input in the following format:

<var>L</var>
<var>A_1</var>
<var>:</var>
<var>A_L</var>

Output

Print the minimum number of operations required when Ringo can freely decide how Snuke walks.

Sample Input 1

4
1
0
2
3

Sample Output 1

1

Assume that Snuke walks as follows:

He starts walking at coordinate <var>3</var> and finishes walking at coordinate <var>4</var>, visiting coordinates <var>3,4,3,2,3,4</var> in this order.

Then, Snuke's four ears will contain <var>0,0,2,3</var> stones, respectively.
Ringo can satisfy the requirement by putting one stone in the first ear.

Sample Input 2

8
2
0
0
2
1
3
4
1

Sample Output 2

3

Sample Input 3

7
314159265
358979323
846264338
327950288
419716939
937510582
0

Sample Output 3

1


Score : <var>200</var> points

Problem Statement

Snuke is interested in strings that satisfy the following conditions:

The length of the string is at least <var>N</var>.

The first <var>N</var> characters equal to the string <var>s</var>.

The last <var>N</var> characters equal to the string <var>t</var>.

Find the length of the shortest string that satisfies the conditions.

Constraints

<var>1≤N≤100</var>

The lengths of <var>s</var> and <var>t</var> are both <var>N</var>.

<var>s</var> and <var>t</var> consist of lowercase English letters.

Input

The input is given from Standard Input in the following format:

<var>N</var>
<var>s</var>
<var>t</var>

Output

Print the length of the shortest string that satisfies the conditions.

Sample Input 1

3
abc
cde

Sample Output 1

5

The shortest string is abcde.

Sample Input 2

1
a
z

Sample Output 2

2

The shortest string is az.

Sample Input 3

4
expr
expr

Sample Output 3

4

The shortest string is expr.

int solution() {
  int n;
  char s[101];
  char t[101];
  scanf("%d%s%s", &n, s, t);
  for (int i = 0; i <= n; i++) {
    bool isOK = true;
    for (int j = 0; j < n; j++) {
      if (i + j < n && s[i + j] != t[j]) {
        isOK = false;
        break;
      }
    }
    if (isOK) {
      printf("%d\n", n + i);
      break;
    }
  }
  return 0;
}

fn solution() {
    let _N: usize = {
        let mut line: String = String::new();
        std::io::stdin().read_line(&mut line).unwrap();
        line.trim().parse().unwrap()
    };
    let s: String = {
        let mut line: String = String::new();
        std::io::stdin().read_line(&mut line).unwrap();
        line.trim().to_string()
    };
    let t: String = {
        let mut line: String = String::new();
        std::io::stdin().read_line(&mut line).unwrap();
        line.trim().to_string()
    };

    let A = s.len();
    let B = t.len();
    let ans = A + B
        - (1..(std::cmp::min(s.len(), t.len()) + 1))
            .rev()
            .find(|&k| s[A - k..] == t[..k])
            .unwrap_or(0);

    println!("{}", ans);
}


Score : <var>600</var> points

Problem Statement

There is an infinitely large pond, which we consider as a number line.
In this pond, there are <var>N</var> lotuses floating at coordinates <var>0</var>, <var>1</var>, <var>2</var>, ..., <var>N-2</var> and <var>N-1</var>.
On the lotus at coordinate <var>i</var>, an integer <var>s_i</var> is written.

You are standing on the lotus at coordinate <var>0</var>. You will play a game that proceeds as follows:

<var>1</var>. Choose positive integers <var>A</var> and <var>B</var>. Your score is initially <var>0</var>.

<var>2</var>. Let <var>x</var> be your current coordinate, and <var>y = x+A</var>. The lotus at coordinate <var>x</var> disappears, and you move to coordinate <var>y</var>.

If <var>y = N-1</var>, the game ends.

If <var>y \neq N-1</var> and there is a lotus floating at coordinate <var>y</var>, your score increases by <var>s_y</var>.

If <var>y \neq N-1</var> and there is no lotus floating at coordinate <var>y</var>, you drown. Your score decreases by <var>10^{100}</var> points, and the game ends.

<var>3</var>. Let <var>x</var> be your current coordinate, and <var>y = x-B</var>. The lotus at coordinate <var>x</var> disappears, and you move to coordinate <var>y</var>.

If <var>y = N-1</var>, the game ends.

If <var>y \neq N-1</var> and there is a lotus floating at coordinate <var>y</var>, your score increases by <var>s_y</var>.

If <var>y \neq N-1</var> and there is no lotus floating at coordinate <var>y</var>, you drown. Your score decreases by <var>10^{100}</var> points, and the game ends.

<var>4</var>. Go back to step <var>2</var>.

You want to end the game with as high a score as possible.
What is the score obtained by the optimal choice of <var>A</var> and <var>B</var>?

Constraints

<var>3 \leq N \leq 10^5</var>

<var>-10^9 \leq s_i \leq 10^9</var>

<var>s_0=s_{N-1}=0</var>

All values in input are integers.

Input

Input is given from Standard Input in the following format:

<var>N</var>
<var>s_0</var> <var>s_1</var> <var>......</var> <var>s_{N-1}</var>

Output

Print the score obtained by the optimal choice of <var>A</var> and <var>B</var>.

Sample Input 1

5
0 2 5 1 0

Sample Output 1

3

If you choose <var>A = 3</var> and <var>B = 2</var>, the game proceeds as follows:

Move to coordinate <var>0 + 3 = 3</var>. Your score increases by <var>s_3 = 1</var>.

Move to coordinate <var>3 - 2 = 1</var>. Your score increases by <var>s_1 = 2</var>.

Move to coordinate <var>1 + 3 = 4</var>. The game ends with a score of <var>3</var>.

There is no way to end the game with a score of <var>4</var> or higher, so the answer is <var>3</var>. Note that you cannot land the lotus at coordinate <var>2</var> without drowning later.

Sample Input 2

6
0 10 -7 -4 -13 0

Sample Output 2

0

The optimal strategy here is to land the final lotus immediately by choosing <var>A = 5</var> (the value of <var>B</var> does not matter).

Sample Input 3

11
0 -4 0 -99 31 14 -15 -39 43 18 0

Sample Output 3

59

int solution() {
  int n;
  int i;
  int j;
  long long d[100010];
  long long s;
  long long m = 0;
  scanf("%d", &n);
  for (i = 0; i < n; i++) {
    scanf("%lld", &d[i]);
  }
  for (i = 1; i < n; i++) {
    for (j = s = 0; i * j < n; j++) {
      if ((n - 1) % i == 0 && i * j >= n - 1 - i * j) {
        break;
      }
      if (n - i * j - 1 < i) {
        break;
      }
      s += d[i * j] + d[n - (i * j) - 1];
      if (m < s) {
        m = s;
      }
    }
  }
  printf("%lld\n", m);
  return 0;
}

fn solution() {
    input! {
        n: usize,
        v: [isize; n]
    }
    let mut ans: isize = 0;
    for c in 1..n - 1 {
        let mut s: isize = 0;
        for i in 1..(n - 1) / c {
            s += v[i * c] + v[n - 1 - i * c];
            if (n - 1) % c == 0 && (n - 1) / c <= 2 * i {
                continue;
            }
            ans = max(ans, s);
        }
    }
    println!("{}", ans);
}


Among Johnny's numerous hobbies, there are two seemingly harmless ones: applying bitwise operations and sneaking into his dad's office. As it is usually the case with small children, Johnny is unaware that combining these two activities can get him in a lot of trouble.There is a set $$$S$$$ containing very important numbers on his dad's desk. The minute Johnny heard about it, he decided that it's a good idea to choose a positive integer $$$k$$$ and replace each element $$$s$$$ of the set $$$S$$$ with $$$s \oplus k$$$ ($$$\oplus$$$ denotes the exclusive or operation). Help him choose such $$$k$$$ that Johnny's dad will not see any difference after his son is done playing (i.e. Johnny will get the same set as before playing). It is possible that no such number exists. It is also possible that there are many of them. In such a case, output the smallest one. Note that the order of elements in a set doesn't matter, i.e. set $$$\{1, 2, 3\}$$$ equals to set $$$\{2, 1, 3\}$$$.Formally, find the smallest positive integer $$$k$$$ such that $$$\{s \oplus k | s \in S\} = S$$$ or report that there is no such number.For example, if $$$S = \{1, 3, 4\}$$$ and $$$k = 2$$$, new set will be equal to $$$\{3, 1, 6\}$$$. If $$$S = \{0, 1, 2, 3\}$$$ and $$$k = 1$$$, after playing set will stay the same.

int solution() {
  long long int t;
  scanf("%lld", &t);
  while (t--) {
    long long int n;
    long long int a[1024];
    scanf("%lld", &n);
    for (long int i = 0; i < n; i++) {
      scanf("%lld", &a[i]);
    }
    long int x = 1;
    long int y;
    long int q = 1;
    while (x < 1025) {
      long int count = 0;
      for (long int i = 0; i < n; i++) {
        y = a[i] ^ x;
        for (long long int j = 0; j < n; j++) {
          if (y == a[j]) {
            count++;
            j = n;
          }
        }
      }
      if (count == n) {
        q = 1;
        break;
      }
      x++;
    }
    if (x == 1025) {
      printf("-1\n");
    } else if (q == 1) {
      printf("%ld\n", x);
    }
  }
}

fn solution() {
    let mut s: String = String::new();
    std::io::stdin().read_to_string(&mut s).ok();
    let mut itr = s.split_whitespace();
    let t: usize = itr.next().unwrap().parse().unwrap();
    let mut out = Vec::new();
    for _ in 0..t {
        let n: usize = itr.next().unwrap().parse().unwrap();
        let mut a: Vec<usize> = (0..n)
            .map(|_| itr.next().unwrap().parse().unwrap())
            .collect();

        a.sort();
        let mut good = false;
        for bit in 1..1 << 10 {
            let mut cmp = vec![0; n];
            for i in 0..n {
                cmp[i] = a[i] ^ bit;
            }
            cmp.sort();
            let mut ok = true;
            for i in 0..n {
                if a[i] != cmp[i] {
                    ok = false;
                }
            }
            if ok {
                writeln!(out, "{}", bit).ok();
                good = true;
                break;
            }
        }
        if !good {
            writeln!(out, "-1").ok();
        }
    }
    stdout().write_all(&out).unwrap();
}


Score: <var>100</var> points

Problem Statement

In AtCoder city, there are five antennas standing in a straight line. They are called Antenna <var>A, B, C, D</var> and <var>E</var> from west to east, and their coordinates are <var>a, b, c, d</var> and <var>e</var>, respectively.

Two antennas can communicate directly if the distance between them is <var>k</var> or less, and they cannot if the distance is greater than <var>k</var>.

Determine if there exists a pair of antennas that cannot communicate directly.

Here, assume that the distance between two antennas at coordinates <var>p</var> and <var>q</var> (<var>p < q</var>) is <var>q - p</var>.

Constraints

<var>a, b, c, d, e</var> and <var>k</var> are integers between <var>0</var> and <var>123</var> (inclusive).

<var>a < b < c < d < e</var>

Input

Input is given from Standard Input in the following format:

<var>a</var>
<var>b</var>
<var>c</var>
<var>d</var>
<var>e</var>
<var>k</var>

Output

Print :( if there exists a pair of antennas that cannot communicate directly, and print Yay! if there is no such pair.

Sample Input 1

1
2
4
8
9
15

Sample Output 1

Yay!

In this case, there is no pair of antennas that cannot communicate directly, because:

the distance between <var>A</var> and <var>B</var> is <var>2 - 1</var> = <var>1</var>

the distance between <var>A</var> and <var>C</var> is <var>4 - 1</var> = <var>3</var>

the distance between <var>A</var> and <var>D</var> is <var>8 - 1</var> = <var>7</var>

the distance between <var>A</var> and <var>E</var> is <var>9 - 1</var> = <var>8</var>

the distance between <var>B</var> and <var>C</var> is <var>4 - 2</var> = <var>2</var>

the distance between <var>B</var> and <var>D</var> is <var>8 - 2</var> = <var>6</var>

the distance between <var>B</var> and <var>E</var> is <var>9 - 2</var> = <var>7</var>

the distance between <var>C</var> and <var>D</var> is <var>8 - 4</var> = <var>4</var>

the distance between <var>C</var> and <var>E</var> is <var>9 - 4</var> = <var>5</var>

the distance between <var>D</var> and <var>E</var> is <var>9 - 8</var> = <var>1</var>

and none of them is greater than <var>15</var>. Thus, the correct output is Yay!.

Sample Input 2

15
18
26
35
36
18

Sample Output 2

:(

In this case, the distance between antennas <var>A</var> and <var>D</var> is <var>35 - 15</var> = <var>20</var> and exceeds <var>18</var>, so they cannot communicate directly.
Thus, the correct output is :(.

fn solution() {
    let mut buf = String::new();
    std::io::stdin().read_line(&mut buf).unwrap();
    let a: usize = buf.trim().parse().unwrap();

    let mut buf = String::new();
    std::io::stdin().read_line(&mut buf).unwrap();
    let _b: usize = buf.trim().parse().unwrap();

    let mut buf = String::new();
    std::io::stdin().read_line(&mut buf).unwrap();
    let _c: usize = buf.trim().parse().unwrap();

    let mut buf = String::new();
    std::io::stdin().read_line(&mut buf).unwrap();
    let _d: usize = buf.trim().parse().unwrap();

    let mut buf = String::new();
    std::io::stdin().read_line(&mut buf).unwrap();
    let e: usize = buf.trim().parse().unwrap();

    let mut buf = String::new();
    std::io::stdin().read_line(&mut buf).unwrap();
    let k: usize = buf.trim().parse().unwrap();

    if e - a > k {
        println!(":(");
    } else {
        println!("Yay!");
    }
}


Score : <var>100</var> points

Problem Statement

We call a <var>4</var>-digit integer with three or more consecutive same digits, such as <var>1118</var>, good.

You are given a <var>4</var>-digit integer <var>N</var>. Answer the question: Is <var>N</var> good?

Constraints

<var>1000 ≤ N ≤ 9999</var>

<var>N</var> is an integer.

Input

Input is given from Standard Input in the following format:

<var>N</var>

Output

If <var>N</var> is good, print Yes; otherwise, print No.

Sample Input 1

1118

Sample Output 1

Yes

<var>N</var> is good, since it contains three consecutive <var>1</var>.

Sample Input 2

7777

Sample Output 2

Yes

An integer is also good when all the digits are the same.

Sample Input 3

1234

Sample Output 3

No

fn solution() {
    let mut buf = String::new();
    std::io::stdin().read_line(&mut buf).ok();
    let d: Vec<char> = buf.trim().chars().collect();
    if d[0] == d[1] && d[1] == d[2] || d[1] == d[2] && d[2] == d[3] {
        println!("Yes");
    } else {
        println! {"No"};
    }
}


You are given an array $$$a$$$ consisting of $$$n$$$ nonnegative integers. Let's define the prefix OR array $$$b$$$ as the array $$$b_i = a_1~\mathsf{OR}~a_2~\mathsf{OR}~\dots~\mathsf{OR}~a_i$$$, where $$$\mathsf{OR}$$$ represents the bitwise OR operation. In other words, the array $$$b$$$ is formed by computing the $$$\mathsf{OR}$$$ of every prefix of $$$a$$$.You are asked to rearrange the elements of the array $$$a$$$ in such a way that its prefix OR array is lexicographically maximum.An array $$$x$$$ is lexicographically greater than an array $$$y$$$ if in the first position where $$$x$$$ and $$$y$$$ differ, $$$x_i > y_i$$$.

int solution(void) {
  int t;
  scanf("%d", &t);
  for (int i = 0; i < t; i++) {
    int n;
    scanf("%d", &n);
    unsigned long long data[n];
    for (int j = 0; j < n; j++) {
      scanf("%llu", &data[j]);
    }
    int settled = 0;
    int alive = n;
    unsigned long long now = 0;
    while (settled < alive) {
      unsigned long long max = now;
      int maxi;
      for (int j = settled; j < alive; j++) {
        unsigned long long result = now | data[j];
        if (result > max) {
          max = result;
          maxi = j;
        } else if (result == now) {
          unsigned long long tmp = data[--alive];
          data[alive] = data[j];
          data[j] = tmp;

          j--;
        }
      }
      if (max != now) {
        unsigned long long tmp = data[maxi];
        data[maxi] = data[settled];
        data[settled++] = tmp;
        now = max;
      }
    }
    for (int j = 0; j < n; j++) {
      printf("%lld ", data[j]);
    }
    printf("\n");
  }
  return 0;
}

fn solution() {
    let t: usize = {
        let mut line: String = String::new();
        std::io::stdin().read_line(&mut line).unwrap();
        line.trim().parse().unwrap()
    };

    for _ in 0..t {
        let n: usize = {
            let mut line: String = String::new();
            std::io::stdin().read_line(&mut line).unwrap();
            line.trim().parse().unwrap()
        };
        let a: Vec<i64> = {
            let mut line: String = String::new();
            std::io::stdin().read_line(&mut line).unwrap();
            line.split_whitespace()
                .map(|x| x.parse().unwrap())
                .collect()
        };

        let mut b = a.clone();
        let mut used = vec![0; n];
        let mut ans = vec![0; 0];

        loop {
            let mut maxvalue = 0;
            let mut maxid = 0;

            for i in 0..n {
                if maxvalue < b[i] {
                    maxvalue = b[i];
                    maxid = i;
                }
            }

            if maxvalue == 0 {
                break;
            }

            ans.push(a[maxid]);
            used[maxid] = 1;
            let xor = ((1 << 32) - 1) ^ b[maxid];

            for i in 0..n {
                b[i] &= xor;
            }
        }

        for i in 0..n {
            if used[i] == 0 {
                ans.push(a[i]);
            }
        }

        println!(
            "{}",
            ans.iter()
                .map(std::string::ToString::to_string)
                .collect::<Vec<_>>()
                .join(" ")
        );
    }
}


Anton's favourite geometric figures are regular polyhedrons. Note that there are five kinds of regular polyhedrons:   Tetrahedron. Tetrahedron has 4 triangular faces.  Cube. Cube has 6 square faces.  Octahedron. Octahedron has 8 triangular faces.  Dodecahedron. Dodecahedron has 12 pentagonal faces.  Icosahedron. Icosahedron has 20 triangular faces. All five kinds of polyhedrons are shown on the picture below:  Anton has a collection of n polyhedrons. One day he decided to know, how many faces his polyhedrons have in total. Help Anton and find this number!

int solution() {
  char poligono[12];
  int n = 0;
  int i = 0;
  int caras = 0;

  scanf("%d", &n);
  for (i = 0; i < n; i++) {
    scanf("%s", poligono);
    if (poligono[0] == 'T') {
      caras = caras + 4;
    } else if (poligono[0] == 'C') {
      caras = caras + 6;
    } else if (poligono[0] == 'O') {
      caras = caras + 8;
    } else if (poligono[0] == 'D') {
      caras = caras + 12;
    } else if (poligono[0] == 'I') {
      caras = caras + 20;
    }
  }
  printf("%d", caras);
  return 0;
}

fn solution() {
    let input = stdin();
    let handle = input.lock();
    let mut lines = handle.lines();

    let count: i32 = lines
        .next()
        .expect("ERROR: next count")
        .expect("Unwrap ended")
        .parse()
        .expect("Error: parse round");

    let mut stack: Vec<u32> = Vec::new();

    for _ in 0..count {
        let line = &lines.next().unwrap().unwrap()[..];
        let face = match line {
            "Tetrahedron" => 4,
            "Cube" => 6,
            "Octahedron" => 8,
            "Dodecahedron" => 12,
            "Icosahedron" => 20,
            _ => 0,
        };
        stack.push(face);
    }
    println!("{}", stack.iter().sum::<u32>());
}


Problem A: Clock

Problem

がっちょ君はお気に入りの時計を持っている。ある日時計の長針が取れてしまい、どこかへなくしてしまった。しかし、がっちょ君はその時計を使い続けたいため短針だけで時刻を読み取りたいと考えている。

短針の情報<var>θ</var>に対する時刻(時<var>h</var>、分<var>m</var>)を出力せよ。時計はいわゆるアナログ時計で、1から12の数が等間隔に時計回りに昇順に並んでいるものである。

Input

入力は以下の形式で与えられる。

<var>θ</var>

1行に短針の角度 <var>θ</var>を表す整数が度数法(degree)で与えられる。
短針が指す方向は、12時を指す方向を0度としたとき、時計回りに<var>θ</var>度回転した方向である。

Constraints

0 ≤ <var>θ</var> ≤ 359

Output

  短針が与えられた角度をとる時の時刻を

  <var>h</var> <var>m</var>

  の形で1行に出力する。

  時刻に午前午後の区別はないため0 ≤ <var>h</var> ≤ 11とする。

Sample Input 1

0

Sample Output 1

0 0

Sample Input 2

45

Sample Output 2

1 30

Sample Input 3

100

Sample Output 3

3 20

int solution() {
  int a;
  scanf("%d", &a);
  printf("%d %d\n", a / 30, a % 30 * 2);
  return 0;
}


English Sentence

Your task is to write a program which reads a text and prints two words. The first one is the word which is arise most frequently in the text. The second one is the word which has the maximum number of letters.

The text includes only alphabetical characters and spaces. A word is a sequence of letters which is separated by the spaces.

Input

A text is given in a line. You can assume the following conditions:

The number of letters in the text is less than or equal to 1000.

 The number of letters in a word is less than or equal to 32.

 There is only one word which is arise most frequently in given text.

 There is only one word which has the maximum number of letters in given text.

Output

The two words separated by a space.

Sample Input

Thank you for your mail and your lectures

Output for the Sample Input

your lectures

int solution() {
  int n = 0;
  int i;
  int cnt[100] = {0};
  int j = 0;
  int max = 0;
  int longer = 0;
  int cou[100] = {0};
  char str;
  char save[100][100];
  while (1) {
    scanf("%c", &str);
    if (str == '\n') {
      break;
    }
    if (str != ' ') {
      if (isupper(str)) {
        str = tolower(str);
      }
      save[j][n] = str;
      n++;
      cou[j]++;
    } else {
      for (i = 0; i < j; i++) {
        if (!strcmp(save[i], save[j])) {
          cnt[i]++;
        }
      }
      j++;
      n = 0;
    }
  }
  for (i = 0; i <= j; i++) {
    if (cnt[max] <= cnt[i]) {
      max = i;
    }
  }
  for (i = 1; i <= j + 1; i++) {
    if (cou[longer] <= cou[i]) {
      longer = i;
    }
  }
  printf("%s %s\n", save[max], save[longer]);

  return 0;
}

fn solution() {
    let mut s = String::new();
    std::io::stdin().read_line(&mut s).unwrap();
    let ss = s.split_whitespace().collect::<Vec<&str>>();

    let mut map_count = HashMap::with_capacity(ss.len());
    for s in ss {
        *map_count.entry(s).or_insert(0) += 1;
    }

    let max_count = {
        let mut ret = String::new();
        let max_value = map_count.values().max().unwrap();
        for (key, value) in &map_count {
            if value != max_value {
                continue;
            }
            ret = key.to_string();
            break;
        }
        ret
    };

    let max_len = map_count
        .keys()
        .max_by(|l, r| l.len().cmp(&r.len()))
        .unwrap();
    println!("{} {}", max_count, max_len);
}


You are given a set of $$$n$$$ ($$$n$$$ is always a power of $$$2$$$) elements containing all integers $$$0, 1, 2, \ldots, n-1$$$ exactly once.Find $$$\frac{n}{2}$$$ pairs of elements such that:  Each element in the set is in exactly one pair.  The sum over all pairs of the bitwise AND of its elements must be exactly equal to $$$k$$$. Formally, if $$$a_i$$$ and $$$b_i$$$ are the elements of the $$$i$$$-th pair, then the following must hold: $$$$$$\sum_{i=1}^{n/2}{a_i \& b_i} = k,$$$$$$ where $$$\&$$$ denotes the bitwise AND operation. If there are many solutions, print any of them, if there is no solution, print $$$-1$$$ instead.

int solution() {
  int t;
  scanf("%d", &t);
  getchar();

  for (int qwq = 0; qwq < t; qwq++) {
    long long n;
    long long k;
    scanf("%lld %lld", &n, &k);
    getchar();
    if (k == 3 && n == 4) {
      printf("-1\n");
      continue;
    }
    if (k == n - 1) {
      printf("0 2\n1 %lld\n", n - 3);
      for (long long i = 3; i < n / 2; i++) {
        printf("%lld %lld\n", i, n - 1 - i);
      }
      printf("%lld %lld\n", k - 1, k);
      continue;
    }
    for (long long i = 0; i < n / 2; i++) {
      if (i != 0 && i != k && i != n - k - 1) {
        printf("%lld %lld\n", i, n - 1 - i);
      } else if (i == 0) {
        printf("%lld %lld\n", i, n - k - 1);
      } else if (i == k || i == n - k - 1) {
        printf("%lld %lld\n", k, n - 1);
      }
    }
  }

  return 0;
}

fn solution() {
    let stdin = io::stdin();
    let mut lines = stdin.lock().lines();
    let t: usize = lines.next().unwrap().unwrap().parse().unwrap();
    for _ in 0..t {
        let xs: Vec<usize> = lines
            .next()
            .unwrap()
            .unwrap()
            .split(' ')
            .map(|x| x.parse().unwrap())
            .collect();
        let n = xs[0];
        let k = xs[1];
        let mask = n - 1;
        if k == mask {
            if n == 4 {
                println!("-1");
            } else {
                println!("0 {}", mask - 1);
                for i in 1..(n / 2 - 1) {
                    let mut m = 1;
                    let mut b = true;
                    while m <= i {
                        if i == m {
                            println!("{} {}", i, n - 2 * i - 1);
                            b = false;
                        }
                        m *= 2;
                    }
                    if b {
                        println!("{} {}", i, n - i - 1);
                    }
                }
                println!("{} {}", n - 1, n / 2);
            }
        } else {
            println!("{} {}", k, mask);
            for i in 1..(n / 2) {
                if i == k {
                    println!("0 {}", n - k - 1);
                } else if i == n - k - 1 {
                    println!("{} 0", n - k - 1);
                } else {
                    println!("{} {}", i, n - i - 1);
                }
            }
        }
    }
}


Score : <var>300</var> points

Problem Statement

Takahashi, Nakahashi and Hikuhashi have integers <var>A</var>, <var>B</var> and <var>C</var>, respectively.
After repeating the following operation <var>K</var> times, find the integer Takahashi will get minus the integer Nakahashi will get:

Each of them simultaneously calculate the sum of the integers that the other two people have, then replace his own integer with the result.

However, if the absolute value of the answer exceeds <var>10^{18}</var>, print Unfair instead.

Constraints

<var>1 \leq A,B,C \leq 10^9</var>

<var>0 \leq K \leq 10^{18}</var>

All values in input are integers.

Input

Input is given from Standard Input in the following format:

<var>A</var> <var>B</var> <var>C</var> <var>K</var>

Output

Print the integer Takahashi will get minus the integer Nakahashi will get, after repeating the following operation <var>K</var> times.
If the absolute value of the answer exceeds <var>10^{18}</var>, print Unfair instead.

Sample Input 1

1 2 3 1

Sample Output 1

1

After one operation, Takahashi, Nakahashi and Hikuhashi have <var>5</var>, <var>4</var> and <var>3</var>, respectively. We should print <var>5-4=1</var>.

Sample Input 2

2 3 2 0

Sample Output 2

-1

Sample Input 3

1000000000 1000000000 1000000000 1000000000000000000

Sample Output 3

0

int solution() {
  int a;
  int b;
  int k;
  scanf("%d%d%*d%d", &a, &b, &k);
  if (k & 1) {
    printf("%d", b - a);
  } else {
    printf("%d", a - b);
  }
}

fn solution() {
    let mut buf = String::new();
    std::io::stdin().read_line(&mut buf).ok();
    let (a, b, _c, k) = {
        let v: Vec<i64> = buf.split_whitespace().map(|x| x.parse().unwrap()).collect();
        (v[0], v[1], v[2], v[3])
    };
    println!("{}", if k % 2 == 0 { a - b } else { b - a });
}


Bit Operation II

  Given two non-negative decimal integers $a$ and $b$, calculate their AND (logical conjunction), OR (logical disjunction) and XOR (exclusive disjunction) and print them in binary representation of 32 bits.

Input

  The input is given in the following format.

$a \; b$

Output

  Print results of AND, OR and  XOR in a line respectively.

Constraints

$0 \leq a, b \leq 2^{32} - 1$

Sample Input 1

8 10

Sample Output 1

00000000000000000000000000001000
00000000000000000000000000001010
00000000000000000000000000000010

int solution() {
  unsigned int a;
  unsigned int b;
  unsigned int n;
  unsigned int m;
  unsigned int o;
  unsigned int x;
  fscanf(stdin, "%u %u", &n, &m);
  char buf[100];
  buf[32] = buf[65] = buf[98] = '\n';
  buf[99] = '\0';
  a = n & m;
  o = n | m;
  x = n ^ m;
  int i = 0;
  for (b = 2147483648U; b > 0; b >>= 1) {
    buf[i] = '0' + a / b;
    buf[i + 33] = '0' + o / b;
    buf[i + 66] = '0' + x / b;
    a %= b;
    o %= b;
    x %= b;
    i++;
  }
  fputs(buf, stdout);
  return 0;
}

fn solution() {
    let mut stdin = io::stdin();
    let mut buf = String::new();
    let _ = stdin.read_to_string(&mut buf);
    let mut iter = buf.split_whitespace();
    let a: u32 = iter.next().unwrap().parse().unwrap();
    let b: u32 = iter.next().unwrap().parse().unwrap();
    println!("{:032b}", a & b);
    println!("{:032b}", a | b);
    println!("{:032b}", a ^ b);
}


Ivan wants to play a game with you. He picked some string $$$s$$$ of length $$$n$$$ consisting only of lowercase Latin letters. You don't know this string. Ivan has informed you about all its improper prefixes and suffixes (i.e. prefixes and suffixes of lengths from $$$1$$$ to $$$n-1$$$), but he didn't tell you which strings are prefixes and which are suffixes.Ivan wants you to guess which of the given $$$2n-2$$$ strings are prefixes of the given string and which are suffixes. It may be impossible to guess the string Ivan picked (since multiple strings may give the same set of suffixes and prefixes), but Ivan will accept your answer if there is at least one string that is consistent with it. Let the game begin!

fn solution() {
    let mut stdin = String::new();
    std::io::Read::read_to_string(&mut std::io::stdin(), &mut stdin).unwrap();
    let mut stdin = stdin.split_whitespace();
    let mut get = || stdin.next().unwrap();

    let n = get!(usize);
    let mut ss = vec![];
    for i in 0..2 * n - 2 {
        let s = get().as_bytes();
        ss.push((s, i, 0));
    }
    ss.sort();
    let mut i = 0;
    let mut guess = vec![0_u8; n];
    while i < ss.len() - 1 {
        if ss[i].0 == ss[i + 1].0 {
            ss[i].2 = 1;
            ss[i + 1].2 = 2;
            for j in 0..ss[i].0.len() {
                guess[j] = ss[i].0[j];
                let k = ss[i].0.len() - 1 - j;
                let j = guess.len() - 1 - j;
                guess[j] = ss[i].0[k];
            }
            i += 2;
        } else {
            i += 1;
        }
    }

    let mut foo = vec![];
    let mut bar = vec![];
    foo.push(guess);
    bar.push(vec![]);

    for i in 0..ss.len() {
        if ss[i].2 != 0 {
            for j in 0..bar.len() {
                bar[j].push(ss[i].2);
            }
            continue;
        }
        let mut tmp1 = vec![];
        let mut tmp2 = vec![];
        for j in 0..foo.len() {
            let f = &foo[j];
            let b = &bar[j];
            let s = &ss[i].0;
            let mut ok = true;
            for k in 0..s.len() {
                if f[k] != 0 && s[k] != f[k] {
                    ok = false;
                    break;
                }
            }
            if ok {
                let mut fc = f.clone();
                let mut bc = b.clone();
                bc.push(1);
                for k in 0..s.len() {
                    fc[k] = s[k];
                }
                tmp1.push(fc);
                tmp2.push(bc);
            }
            let mut ok = true;
            for k in 0..s.len() {
                let r = f.len() - 1 - k;
                let k = s.len() - 1 - k;
                if f[r] != 0 && s[k] != f[r] {
                    ok = false;
                    break;
                }
            }
            if ok {
                let mut fc = f.clone();
                let mut bc = b.clone();
                bc.push(2);
                for k in 0..s.len() {
                    let r = f.len() - 1 - k;
                    let k = s.len() - 1 - k;
                    fc[r] = s[k];
                }
                tmp1.push(fc);
                tmp2.push(bc);
            }
        }
        foo = tmp1;
        bar = tmp2;
    }

    for b in bar.iter() {
        let mut c1 = 0;
        let mut c2 = 0;
        for e in b.iter() {
            if *e == 1 {
                c1 += 1;
            } else {
                c2 += 1;
            }
        }
        if c1 != c2 {
            continue;
        }
        let mut ans = vec![0; ss.len()];
        for i in 0..ss.len() {
            ans[ss[i].1] = b[i];
        }
        for a in ans.iter() {
            if *a == 1 {
                print!("P");
            } else {
                print!("S");
            }
        }
        println!();
        break;
    }
}


Score: <var>100</var> points

Problem Statement

There is a train going from Station <var>A</var> to Station <var>B</var> that costs <var>X</var> yen (the currency of Japan).

Also, there is a bus going from Station <var>B</var> to Station <var>C</var> that costs <var>Y</var> yen.

Joisino got a special ticket. With this ticket, she can take the bus for half the fare if she travels from Station <var>A</var> to Station <var>B</var> by train and then travels from Station <var>B</var> to Station <var>C</var> by bus.

How much does it cost to travel from Station <var>A</var> to Station <var>C</var> if she uses this ticket?

Constraints

<var>1 \leq X,Y \leq 100</var>

<var>Y</var> is an even number.

All values in input are integers.

Input

Input is given from Standard Input in the following format:

<var>X</var> <var>Y</var>

Output

If it costs <var>x</var> yen to travel from Station <var>A</var> to Station <var>C</var>, print <var>x</var>.

Sample Input 1

81 58

Sample Output 1

110

The train fare is <var>81</var> yen.

The train fare is <var>58</var> ⁄ <var>2=29</var> yen with the <var>50</var>% discount.

Thus, it costs <var>110</var> yen to travel from Station <var>A</var> to Station <var>C</var>.

Sample Input 2

4 54

Sample Output 2

31

int solution() {
  int x = 0;
  int y = 0;

  scanf("%d %d", &x, &y);

  printf("%d", x + (y / 2));

  return 0;
}

fn solution() {
    let scan = std::io::stdin();

    let mut line = String::new();

    let _ = scan.read_line(&mut line);

    let vec: Vec<&str> = line.split_whitespace().collect();

    let n: u32 = vec[0].parse().unwrap_or(0);
    let m: u32 = vec[1].parse().unwrap_or(0);
    println!("{}", n + m / 2);
}


Petya has a rectangular Board of size $$$n \times m$$$. Initially, $$$k$$$ chips are placed on the board, $$$i$$$-th chip is located in the cell at the intersection of $$$sx_i$$$-th row and $$$sy_i$$$-th column.In one action, Petya can move all the chips to the left, right, down or up by $$$1$$$ cell.If the chip was in the $$$(x, y)$$$ cell, then after the operation:   left, its coordinates will be $$$(x, y - 1)$$$;  right, its coordinates will be $$$(x, y + 1)$$$;  down, its coordinates will be $$$(x + 1, y)$$$;  up, its coordinates will be $$$(x - 1, y)$$$. If the chip is located by the wall of the board, and the action chosen by Petya moves it towards the wall, then the chip remains in its current position.Note that several chips can be located in the same cell.For each chip, Petya chose the position which it should visit. Note that it's not necessary for a chip to end up in this position.Since Petya does not have a lot of free time, he is ready to do no more than $$$2nm$$$ actions.You have to find out what actions Petya should do so that each chip visits the position that Petya selected for it at least once. Or determine that it is not possible to do this in $$$2nm$$$ actions.

int solution() {
  int n;
  int m;
  scanf("%d %d", &n, &m);
  printf("%d\n", n + m - 3 + (n * m));
  for (int i = 0; i < m - 1; i++) {
    printf("L");
  }
  for (int i = 0; i < n - 1; i++) {
    printf("D");
  }
  for (int i = 0; i < n; i++) {
    if (i != 0) {
      printf("U");
    }
    for (int j = 0; j < m - 1; j++) {
      if (i % 2) {
        printf("L");
      } else {
        printf("R");
      }
    }
  }
}

fn solution() {
    let mut stdin = BufReader::new(io::stdin());
    let mut stdout = BufWriter::new(io::stdout());

    let mut buffer = String::new();
    stdin.read_line(&mut buffer).unwrap();
    let buffer: Vec<i32> = buffer
        .trim()
        .split_ascii_whitespace()
        .map(|x| x.parse().unwrap())
        .collect();
    let (n, m, k) = (buffer[0], buffer[1], buffer[2]);
    for _ in 0..k {
        let mut buffer = String::new();
        stdin.read_line(&mut buffer).unwrap();
    }
    for _ in 0..k {
        let mut buffer = String::new();
        stdin.read_line(&mut buffer).unwrap();
    }

    let mut rls: Vec<String> = Vec::new();

    for _ in 1..n {
        rls.push("D".to_string());
    }
    for _ in 1..m {
        rls.push("L".to_string());
    }

    for i in 0..n {
        if i % 2 == 0 {
            for _ in 1..m {
                rls.push("R".to_string());
            }
        } else {
            for _ in 1..m {
                rls.push("L".to_string());
            }
        }
        rls.push("U".to_string());
    }
    rls.pop();

    writeln!(stdout, "{}", rls.len()).unwrap();
    writeln!(stdout, "{}", rls.join("")).unwrap();
}


There are n incoming messages for Vasya. The i-th message is going to be received after ti minutes. Each message has a cost, which equals to A initially. After being received, the cost of a message decreases by B each minute (it can become negative). Vasya can read any message after receiving it at any moment of time. After reading the message, Vasya's bank account receives the current cost of this message. Initially, Vasya's bank account is at 0.Also, each minute Vasya's bank account receives C·k, where k is the amount of received but unread messages.Vasya's messages are very important to him, and because of that he wants to have all messages read after T minutes.Determine the maximum amount of money Vasya's bank account can hold after T minutes.

int solution() {
  int n;
  int a;
  int b;
  int c;
  int t;
  scanf("%d%d%d%d%d", &n, &a, &b, &c, &t);

  int time[n];
  for (int i = 0; i < n; ++i) {
    scanf("%d", &time[i]);
  }

  int ans = 0;

  if (b >= c) {
    printf("%d", n * a);
  }

  else {
    ans = n * a;
    int x = 0;
    for (int i = 0; i < n; ++i) {
      x = x + t - time[i];
    }

    ans = ans + (c - b) * x;

    printf("%d", ans);
  }

  return 0;
}

fn solution() {
    let mut input = String::new();
    use std::io;
    use std::io::prelude::*;
    io::stdin().read_to_string(&mut input).unwrap();

    let mut it = input.split_whitespace();
    let n: i64 = it.next().unwrap().parse().unwrap();
    let a: i64 = it.next().unwrap().parse().unwrap();
    let b: i64 = it.next().unwrap().parse().unwrap();
    let c: i64 = it.next().unwrap().parse().unwrap();
    let t: i64 = it.next().unwrap().parse().unwrap();

    let tl: Vec<i64> = it.map(|x| x.parse().unwrap()).collect();

    let ans = if c < b {
        n * a
    } else {
        tl.into_iter().map(|ti| a + (t - ti) * (c - b)).sum()
    };

    println!("{}", ans);
}


Score : <var>700</var> points

Problem Statement

Snuke is buying a lamp.
The light of the lamp can be adjusted to <var>m</var> levels of brightness, represented by integers from <var>1</var> through <var>m</var>, by the two buttons on the remote control.

The first button is a "forward" button. When this button is pressed, the brightness level is increased by <var>1</var>, except when the brightness level is <var>m</var>, in which case the brightness level becomes <var>1</var>.

The second button is a "favorite" button. When this button is pressed, the brightness level becomes the favorite brightness level <var>x</var>, which is set when the lamp is purchased.

Snuke is thinking of setting the favorite brightness level <var>x</var> so that he can efficiently adjust the brightness.
He is planning to change the brightness <var>n-1</var> times. In the <var>i</var>-th change, the brightness level is changed from <var>a_i</var> to <var>a_{i+1}</var>. The initial brightness level is <var>a_1</var>.
Find the number of times Snuke needs to press the buttons when <var>x</var> is set to minimize this number.

Constraints

<var>2 \leq n,m \leq 10^5</var>

<var>1 \leq a_i\leq m</var>

<var>a_i \neq a_{i+1}</var>

<var>n</var>, <var>m</var> and <var>a_i</var> are integers.

Input

Input is given from Standard Input in the following format:

<var>n</var> <var>m</var>
<var>a_1</var> <var>a_2</var> … <var>a_n</var>

Output

Print the minimum number of times Snuke needs to press the buttons.

Sample Input 1

4 6
1 5 1 4

Sample Output 1

5

When the favorite brightness level is set to <var>1</var>, <var>2</var>, <var>3</var>, <var>4</var>, <var>5</var> and <var>6</var>, Snuke needs to press the buttons <var>8</var>, <var>9</var>, <var>7</var>, <var>5</var>, <var>6</var> and <var>9</var> times, respectively.
Thus, Snuke should set the favorite brightness level to <var>4</var>.
In this case, the brightness is adjusted as follows:

In the first change, press the favorite button once, then press the forward button once.

In the second change, press the forward button twice.

In the third change, press the favorite button once.

Sample Input 2

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

Sample Output 2

45

int solution() {
  int i;
  int n;
  int m;
  int a;
  int b;
  int diff;
  long long sum = 0;
  long long add[100001][2] = {};
  scanf("%d %d", &n, &m);
  scanf("%d", &a);
  a--;
  for (i = 2; i <= n; i++) {
    scanf("%d", &b);
    b--;
    diff = (b - a + m) % m;
    sum += diff;
    if (diff >= 2) {
      a = (a + 2) % m;
      b = (b + 1) % m;
      add[a][0]++;
      add[a][1]++;
      add[b][0]--;
      add[b][1] -= diff;
      if (b < a) {
        add[0][0]++;
        add[0][1] += m - a + 1;
      }
      a = (b + m - 1) % m;
    } else {
      a = b;
    }
  }

  long long tmp[2] = {};
  long long max = 0;
  for (i = 0; i < m; i++) {
    tmp[0] += add[i][0];
    tmp[1] += add[i][1];
    if (tmp[1] > max) {
      max = tmp[1];
    }
    tmp[1] += tmp[0];
  }
  printf("%lld\n", sum - max);
  fflush(stdout);
  return 0;
}

fn solution() {
    input!(n: usize, m: usize, a: [usize1; n]);
    let mut start = vec![vec![]; m];
    let mut end = vec![vec![]; m];
    for i in 0..(n - 1) {
        let from = a[i];
        let to = a[i + 1];
        start[from].push(to);
        end[to].push(from);
    }

    let mut total = 0;
    let mut count = 0;
    for from in 0..m {
        for &to in start[from].iter() {
            if from > to {
                count += 1;
                total += to + 1;
            } else {
                total += to - from;
            }
        }
    }
    let mut ans = total;
    for cur in 1..m {
        for &from in end[cur - 1].iter() {
            count -= 1;
            let to = cur - 1;
            total += (to + m - from) % m;
            total -= 1;
        }
        total -= count;

        ans = cmp::min(ans, total);
        count += start[cur - 1].len();
    }
    println!("{}", ans);
}


Alice and Bob are playing a game. They start with a positive integer $$$n$$$ and take alternating turns doing operations on it. Each turn a player can subtract from $$$n$$$ one of its divisors that isn't $$$1$$$ or $$$n$$$. The player who cannot make a move on his/her turn loses. Alice always moves first.Note that they subtract a divisor of the current number in each turn.You are asked to find out who will win the game if both players play optimally.

int solution() {
  int t;
  scanf("%d", &t);
  while (t--) {
    int n;
    int flag = 0;
    scanf("%d", &n);
    if (n % 2 == 1) {
      printf("Bob\n");
      continue;
    }
    while (n > 1) {
      if (n % 2 == 1) {
        flag = -2;
        break;
      }
      n = n / 2;
      flag++;
    }
    if (flag == -2) {
      printf("Alice\n");
      continue;
    }
    if (flag % 2 == 0) {
      printf("Alice\n");
      continue;
    }
    if (flag % 2 == 1) {
      printf("Bob\n");
      continue;
    }
  }
  return 0;
}

fn solution() {
    let std_in = stdin();
    let in_lock = std_in.lock();
    let input = BufReader::new(in_lock);

    let std_out = stdout();
    let out_lock = std_out.lock();
    let mut output = BufWriter::new(out_lock);

    let mut lines = input.lines().map(|r| r.unwrap());

    let s = lines.next().unwrap();
    let mut it = s.split_whitespace().map(|s| s.parse::<usize>().unwrap());

    let t = it.next().unwrap();

    for _case_index in 1..=t {
        let s = lines.next().unwrap();
        let mut it = s.split_whitespace().map(|s| s.parse::<usize>().unwrap());

        let n = it.next().unwrap();

        let alice_wins = if n % 2 == 0 {
            if n.count_ones() > 1 {
                true
            } else {
                n.trailing_zeros() % 2 == 0
            }
        } else {
            false
        };

        let ans = if alice_wins { "Alice" } else { "Bob" };

        writeln!(&mut output, "{}", ans).unwrap();
    }
}


You are given an array $$$a$$$ consisting of $$$n$$$ integers.Your task is to determine if $$$a$$$ has some subsequence of length at least $$$3$$$ that is a palindrome.Recall that an array $$$b$$$ is called a subsequence of the array $$$a$$$ if $$$b$$$ can be obtained by removing some (possibly, zero) elements from $$$a$$$ (not necessarily consecutive) without changing the order of remaining elements. For example, $$$[2]$$$, $$$[1, 2, 1, 3]$$$ and $$$[2, 3]$$$ are subsequences of $$$[1, 2, 1, 3]$$$, but $$$[1, 1, 2]$$$ and $$$[4]$$$ are not.Also, recall that a palindrome is an array that reads the same backward as forward. In other words, the array $$$a$$$ of length $$$n$$$ is the palindrome if $$$a_i = a_{n - i - 1}$$$ for all $$$i$$$ from $$$1$$$ to $$$n$$$. For example, arrays $$$[1234]$$$, $$$[1, 2, 1]$$$, $$$[1, 3, 2, 2, 3, 1]$$$ and $$$[10, 100, 10]$$$ are palindromes, but arrays $$$[1, 2]$$$ and $$$[1, 2, 3, 1]$$$ are not.You have to answer $$$t$$$ independent test cases.

int solution() {
  int t = 0;
  scanf("%d", &t);
  for (int i = 1; i <= t; i++) {
    int size = 0;
    scanf("%d", &size);
    int arr[size];
    for (int j = 0; j < size; j++) {
      scanf("%d", &arr[j]);
    }
    int flag = 0;
    for (int j = 0; j <= size - 3; j++) {
      for (int k = j + 2; k < size; k++) {
        if (arr[k] == arr[j]) {
          flag = 1;
          break;
        }
      }
      if (flag == 1) {
        break;
      }
    }
    if (flag == 1) {
      printf("YES\n");
    } else {
      printf("NO\n");
    }
  }
  return 0;
}

fn solution() {
    let mut t = String::new();
    stdin().read_line(&mut t).unwrap();
    for _i in 0..t.trim().parse::<u32>().unwrap() {
        let mut n = String::new();
        stdin().read_line(&mut n).unwrap();
        n = String::new();
        stdin().read_line(&mut n).unwrap();
        let n: Vec<_> = n
            .trim()
            .split(' ')
            .map(|i| i.parse::<i32>().unwrap())
            .collect();
        if n.len() < 3 {
            println!("NO");
            continue;
        }
        let mut b = false;
        for (j, iter) in n[..n.len() - 2].iter().enumerate() {
            if n[j + 2..].contains(iter) {
                println!("YES");
                b = true;
                break;
            }
        }
        if !b {
            println!("NO")
        }
    }
}


Stepan has the newest electronic device with a display. Different digits can be shown on it. Each digit is shown on a seven-section indicator like it is shown on the picture below.  So, for example, to show the digit 3 on the display, 5 sections must be highlighted; and for the digit 6, 6 sections must be highlighted. The battery of the newest device allows to highlight at most n sections on the display. Stepan wants to know the maximum possible integer number which can be shown on the display of his newest device. Your task is to determine this number. Note that this number must not contain leading zeros. Assume that the size of the display is enough to show any integer.

fn solution() {
    let mut n = String::new();

    std::io::stdin()
        .read_line(&mut n)
        .expect("Reading n error!");

    let mut n: u32 = n.trim().parse().expect("Error while parsing!");

    while n > 0 {
        if n % 2 == 1 {
            print!("7");
            n -= 3;
        } else {
            print!("1");
            n -= 2;
        }
    }
}


Score : <var>100</var> points

Problem Statement

It is September <var>9</var> in Japan now.

You are given a two-digit integer <var>N</var>. Answer the question: Is <var>9</var> contained in the decimal notation of <var>N</var>?

Constraints

<var>10≤N≤99</var>

Input

Input is given from Standard Input in the following format:

<var>N</var>

Output

If <var>9</var> is contained in the decimal notation of <var>N</var>, print Yes; if not, print No.

Sample Input 1

29

Sample Output 1

Yes

The one's digit of <var>29</var> is <var>9</var>.

Sample Input 2

72

Sample Output 2

No

<var>72</var> does not contain <var>9</var>.

Sample Input 3

91

Sample Output 3

Yes

int solution(void) {
  char no[100];

  scanf("%s", no);

  if (no[0] != '9' && no[1] != '9') {
    printf("No\n");
  } else {
    printf("Yes\n");
  }

  return 0;
}

fn solution() {
    let mut buffer = String::new();
    let _ = io::stdin().read_to_string(&mut buffer);
    print!(
        "{}",
        if buffer.chars().any(|c| c == '9') {
            "Yes"
        } else {
            "No"
        }
    );
}


To improve the boomerang throwing skills of the animals, Zookeeper has set up an $$$n \times n$$$ grid with some targets, where each row and each column has at most $$$2$$$ targets each. The rows are numbered from $$$1$$$ to $$$n$$$ from top to bottom, and the columns are numbered from $$$1$$$ to $$$n$$$ from left to right.  For each column, Zookeeper will throw a boomerang from the bottom of the column (below the grid) upwards. When the boomerang hits any target, it will bounce off, make a $$$90$$$ degree turn to the right and fly off in a straight line in its new direction. The boomerang can hit multiple targets and does not stop until it leaves the grid.   In the above example, $$$n=6$$$ and the black crosses are the targets. The boomerang in column $$$1$$$ (blue arrows) bounces $$$2$$$ times while the boomerang in column $$$3$$$ (red arrows) bounces $$$3$$$ times. The boomerang in column $$$i$$$ hits exactly $$$a_i$$$ targets before flying out of the grid. It is known that $$$a_i \leq 3$$$.However, Zookeeper has lost the original positions of the targets. Thus, he asks you to construct a valid configuration of targets that matches the number of hits for each column, or tell him that no such configuration exists. If multiple valid configurations exist, you may print any of them.

fn solution() {
    let mut str = String::new();
    let _ = stdin().read_line(&mut str).unwrap();
    let n: usize = str.trim().parse().unwrap();
    str.clear();
    let _ = stdin().read_line(&mut str).unwrap();
    let throws: Vec<usize> = str
        .split_whitespace()
        .map(|x| x.parse().unwrap())
        .rev()
        .collect();
    let mut heights = vec![0; n];
    let mut one: usize = 0;
    let mut line: usize = 0;
    let mut two: usize = 0;
    let mut flag = true;
    let mut xy: Vec<(usize, usize)> = Vec::new();
    for i in 0..n {
        match throws[i] {
            1 => {
                heights[i] = line;
                line += 1;
                xy.push((i, heights[i]));
            }
            2 => {
                while one <= i {
                    if throws[one] == 1 {
                        break;
                    }
                    one += 1;
                }
                if one > i {
                    flag = false;
                    break;
                } else {
                    heights[i] = heights[one];
                    xy.push((i, heights[i]));
                    one += 1;
                }
            }
            3 => {
                while two < i {
                    if throws[two] > 1 {
                        break;
                    }
                    two += 1;
                }
                let mut bounce = two;
                if two >= i {
                    while one <= i {
                        if throws[one] == 1 {
                            break;
                        }
                        one += 1;
                    }
                    if one > i {
                        flag = false;
                        break;
                    }
                    bounce = one;
                    one += 1;
                } else {
                    two += 1;
                }
                heights[i] = line;
                line += 1;
                xy.push((i, heights[i]));
                xy.push((bounce, heights[i]));
            }
            _ => {
                continue;
            }
        }
    }
    if flag {
        println!("{}", xy.len());
        for (a, b) in xy {
            println!("{} {}", n - b, n - a);
        }
    } else {
        println!("-1");
    }
}


A binary matrix is called good if every even length square sub-matrix has an odd number of ones. Given a binary matrix $$$a$$$ consisting of $$$n$$$ rows and $$$m$$$ columns, determine the minimum number of cells you need to change to make it good, or report that there is no way to make it good at all. All the terms above have their usual meanings — refer to the Notes section for their formal definitions.

fn solution() {
    let stdout = io::stdout();
    let mut output = BufWriter::new(stdout.lock());
    let mut stdin = io::stdin();
    let mut input_string = String::new();
    stdin.read_to_string(&mut input_string).unwrap();
    let mut input_iter = input_string.split_whitespace();

    let n: usize = read!();
    let m: usize = read!();
    let mut matrix = Vec::with_capacity(n);

    for _ in 0..n {
        let row_string: String = read!();
        let row = row_string.chars().collect::<Vec<char>>();
        matrix.push(row);
    }

    if n == 1 || m == 1 {
        println!("0");
        return;
    }

    if n > 3 && m > 3 {
        println!("-1");
        return;
    }

    if m > n {
        let matrix_clone = matrix;
        matrix = vec![vec!['_'; n]; m];
        for i in 0..n {
            for j in 0..m {
                matrix[j][i] = matrix_clone[i][j];
            }
        }
    }

    let decimal_vector: Vec<u32> = matrix
        .iter()
        .map(|row| row.iter().collect::<String>())
        .map(|row_str| u32::from_str_radix(&row_str, 2).unwrap())
        .collect();

    let mut possible_adjacent_values = vec![vec![(0, 0); 8]; 4];

    possible_adjacent_values[2][0b_00] = (0b_01, 0b_10);
    possible_adjacent_values[2][0b_01] = (0b_00, 0b_11);
    possible_adjacent_values[2][0b_10] = (0b_00, 0b_11);
    possible_adjacent_values[2][0b_11] = (0b_01, 0b_10);

    possible_adjacent_values[3][0b_000] = (0b_010, 0b_101);
    possible_adjacent_values[3][0b_001] = (0b_011, 0b_100);
    possible_adjacent_values[3][0b_010] = (0b_000, 0b_111);
    possible_adjacent_values[3][0b_011] = (0b_001, 0b_110);
    possible_adjacent_values[3][0b_100] = (0b_001, 0b_110);
    possible_adjacent_values[3][0b_101] = (0b_000, 0b_111);
    possible_adjacent_values[3][0b_110] = (0b_011, 0b_100);
    possible_adjacent_values[3][0b_111] = (0b_010, 0b_101);

    let width = min(n, m);
    let mut mask_upperbound = 8;
    if width == 2 {
        mask_upperbound = 4;
    }

    let mut min_cost = vec![vec![0; mask_upperbound]; decimal_vector.len()];

    for mask in 0..mask_upperbound {
        min_cost[0][mask] = ((mask as u32) ^ decimal_vector[0]).count_ones();
    }

    for i in 1..decimal_vector.len() {
        for mask in 0..mask_upperbound {
            let change_to_mask_cost = ((mask as u32) ^ decimal_vector[i]).count_ones();
            let (a, b) = possible_adjacent_values[width][mask];
            min_cost[i][mask] = change_to_mask_cost + min(min_cost[i - 1][a], min_cost[i - 1][b]);
        }
    }

    println!(
        "{}",
        min_cost[decimal_vector.len() - 1].iter().min().unwrap()
    );
}


You are the gym teacher in the school.There are $$$n$$$ students in the row. And there are two rivalling students among them. The first one is in position $$$a$$$, the second in position $$$b$$$. Positions are numbered from $$$1$$$ to $$$n$$$ from left to right.Since they are rivals, you want to maximize the distance between them. If students are in positions $$$p$$$ and $$$s$$$ respectively, then distance between them is $$$|p - s|$$$. You can do the following operation at most $$$x$$$ times: choose two adjacent (neighbouring) students and swap them.Calculate the maximum distance between two rivalling students after at most $$$x$$$ swaps.

int solution() {

  int t = 0;
  int n = 0;
  int x = 0;
  int a = 0;
  int b = 0;

  int i = 0;
  int j = 0;

  int nminusbig = 0;
  int smallminusone = 0;
  int modified = 0;
  int finaldistance = 0;
  int nsdistance = 0;

  scanf("%d", &t);

  for (i = 0; i < t; i++) {
    scanf("%d %d %d %d", &n, &x, &a, &b);

    if (a < b) {

      nminusbig = n - b;
      smallminusone = a - 1;

      if (nminusbig == 0) {

        if (smallminusone == 0) {
          finaldistance = b - a;
        } else if (smallminusone >= x) {
          modified = a - x;

          finaldistance = (b - modified);
        } else if (smallminusone < x) {
          nsdistance = nminusbig + smallminusone;
          if (nsdistance >= x) {
            finaldistance = (b - a + x);
          } else if (nsdistance < x) {
            finaldistance = (n - 1);
          }
        }
      } else if (nminusbig >= x) {
        modified = b + x;

        finaldistance = modified - a;
      } else if (nminusbig < x) {

        if (smallminusone >= x) {
          modified = a - x;

          finaldistance = (b - modified);
        } else if (smallminusone < x) {
          nsdistance = nminusbig + smallminusone;
          if (nsdistance >= x) {
            finaldistance = (b - a + x);
          } else if (nsdistance < x) {
            finaldistance = (n - 1);
          }
        }
      }
    } else {

      nminusbig = n - a;
      smallminusone = b - 1;

      if (nminusbig == 0) {

        if (smallminusone == 0) {
          finaldistance = a - b;
        } else if (smallminusone >= x) {
          modified = b - x;

          finaldistance = (a - modified);
        } else if (smallminusone < x) {
          nsdistance = nminusbig + smallminusone;
          if (nsdistance >= x) {
            finaldistance = (a - b + x);
          } else {
            finaldistance = (n - 1);
          }
        }
      } else if (nminusbig >= x) {
        modified = a + x;

        finaldistance = modified - b;
      } else if (nminusbig < x) {

        if (smallminusone >= x) {
          modified = b - x;

          finaldistance = (a - modified);
        } else if (smallminusone < x) {
          nsdistance = nminusbig + smallminusone;
          if (nsdistance >= x) {
            finaldistance = (a - b + x);
          } else {
            finaldistance = (n - 1);
          }
        }
      }
    }

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

fn solution() {
    let mut buffer = String::new();
    io::stdin()
        .read_line(&mut buffer)
        .expect("Failed to read line");
    let num_queries = buffer.trim().parse::<i32>().expect("parse error");

    for _i in 0..num_queries {
        buffer.clear();
        io::stdin()
            .read_line(&mut buffer)
            .expect("Failed to read line");
        let values = buffer
            .split_whitespace()
            .map(|x| x.parse::<i32>())
            .collect::<Result<Vec<i32>, _>>()
            .unwrap();

        assert!(values.len() == 4);
        let n = values[0];
        let x = values[1];
        let a = values[2];
        let b = values[3];

        let max_dist = cmp::min(a, b) - 1 + n - cmp::max(a, b);
        let acc_dist = (a - b).abs();
        let acc_max_sep = acc_dist + cmp::min(max_dist, x);

        println!("{}", acc_max_sep);
    }
}


There are two popular keyboard layouts in Berland, they differ only in letters positions. All the other keys are the same. In Berland they use alphabet with 26 letters which coincides with English alphabet.You are given two strings consisting of 26 distinct letters each: all keys of the first and the second layouts in the same order. You are also given some text consisting of small and capital English letters and digits. It is known that it was typed in the first layout, but the writer intended to type it in the second layout. Print the text if the same keys were pressed in the second layout.Since all keys but letters are the same in both layouts, the capitalization of the letters should remain the same, as well as all other characters.

fn solution() {
    let mut fl = String::new();

    stdin().read_line(&mut fl).unwrap();

    let fl = fl.trim();

    let mut sl = String::new();

    stdin().read_line(&mut sl).unwrap();

    let sl = sl.trim();

    let mut txt = String::new();

    stdin().read_line(&mut txt).unwrap();

    let txt = txt.trim();

    let hm: HashMap<_, _> = fl.chars().zip(sl.chars()).collect();

    let txt: String = txt
        .chars()
        .map(|c| {
            if let Some(&d) = hm.get(&c) {
                d
            } else if let Some(&d) = hm.get(&c.to_lowercase().next().unwrap()) {
                d.to_uppercase().next().unwrap()
            } else {
                c
            }
        })
        .collect();

    println!("{}", txt);
}


Score : <var>100</var> points

Problem Statement

We have three boxes <var>A</var>, <var>B</var>, and <var>C</var>, each of which contains an integer.

Currently, the boxes <var>A</var>, <var>B</var>, and <var>C</var> contain the integers <var>X</var>, <var>Y</var>, and <var>Z</var>, respectively.

We will now do the operations below in order. Find the content of each box afterward.

Swap the contents of the boxes <var>A</var> and <var>B</var>

Swap the contents of the boxes <var>A</var> and <var>C</var>

Constraints

<var>1 \leq X,Y,Z \leq 100</var>

All values in input are integers.

Input

Input is given from Standard Input in the following format:

<var>X</var> <var>Y</var> <var>Z</var>

Output

Print the integers contained in the boxes <var>A</var>, <var>B</var>, and <var>C</var>, in this order, with space in between.

Sample Input 1

1 2 3

Sample Output 1

3 1 2

After the contents of the boxes <var>A</var> and <var>B</var> are swapped, <var>A</var>, <var>B</var>, and <var>C</var> contain <var>2</var>, <var>1</var>, and <var>3</var>, respectively.

Then, after the contents of <var>A</var> and <var>C</var> are swapped, <var>A</var>, <var>B</var>, and <var>C</var> contain <var>3</var>, <var>1</var>, and <var>2</var>, respectively.

Sample Input 2

100 100 100

Sample Output 2

100 100 100

Sample Input 3

41 59 31

Sample Output 3

31 41 59

int solution(void) {
  int X = 0;
  int Y = 0;
  int Z = 0;

  scanf("%d %d %d", &X, &Y, &Z);

  printf("%d %d %d", Z, X, Y);

  return 0;
}

fn solution() {
    let mut s = String::new();
    std::io::stdin().read_line(&mut s).unwrap();
    let mut v: Vec<_> = s
        .split_whitespace()
        .map(|x| x.parse::<i32>().unwrap())
        .collect();
    v.swap(0, 1);
    v.swap(0, 2);

    println!("{} {} {}", v[0], v[1], v[2]);
}


Score : <var>200</var> points

Problem Statement

Snuke signed up for a new website which holds programming competitions.
He worried that he might forget his password, and he took notes of it.
Since directly recording his password would cause him trouble if stolen,
he took two notes: one contains the characters at the odd-numbered positions, and the other contains the characters at the even-numbered positions.

You are given two strings <var>O</var> and <var>E</var>. <var>O</var> contains the characters at the odd-numbered positions retaining their relative order, and <var>E</var> contains the characters at the even-numbered positions retaining their relative order.
Restore the original password.

Constraints

<var>O</var> and <var>E</var> consists of lowercase English letters (a - z).

<var>1 \leq |O|,|E| \leq 50</var>

<var>|O| - |E|</var> is either <var>0</var> or <var>1</var>.

Input

Input is given from Standard Input in the following format:

<var>O</var>
<var>E</var>

Output

Print the original password.

Sample Input 1

xyz
abc

Sample Output 1

xaybzc

The original password is xaybzc. Extracting the characters at the odd-numbered positions results in xyz, and extracting the characters at the even-numbered positions results in abc.

Sample Input 2

atcoderbeginnercontest
atcoderregularcontest

Sample Output 2

aattccooddeerrbreeggiunlnaerrccoonntteesstt

fn solution() {
    let mut a = String::new();
    let mut b = String::new();
    let mut s = String::new();

    std::io::stdin().read_line(&mut a).ok();
    std::io::stdin().read_line(&mut b).ok();

    let a = a.chars().collect::<Vec<char>>();
    let b = b.chars().collect::<Vec<char>>();

    for i in 0..a.len() + b.len() {
        if i % 2 == 0 {
            s.push(a[i / 2]);
        } else {
            s.push(b[i / 2]);
        }
    }
    println!("{}", &s);
}


Score : <var>100</var> points

Problem Statement

Snuke has <var>N</var> integers: <var>1,2,\ldots,N</var>.
He will choose <var>K</var> of them and give those to Takahashi.

How many ways are there to choose <var>K</var> consecutive integers?

Constraints

All values in input are integers.

<var>1 \leq K \leq N \leq 50</var>

Input

Input is given from Standard Input in the following format:

<var>N</var> <var>K</var>

Output

Print the answer.

Sample Input 1

3 2

Sample Output 1

2

There are two ways to choose two consecutive integers: <var>(1,2)</var> and <var>(2,3)</var>.

Sample Input 2

13 3

Sample Output 2

11

int solution() {
  int n = 0;
  int k = 0;
  scanf("%d %d", &n, &k);
  printf("%d\n", n - k + 1);
  return 0;
}

fn solution() {
    let mut buf = String::new();

    std::io::stdin().read_to_string(&mut buf).unwrap();

    let mut iter = buf.split_whitespace();

    let n: usize = iter.next().unwrap().parse().unwrap();

    let k: usize = iter.next().unwrap().parse().unwrap();

    let ans: usize = n - k + 1;

    let out = stdout();
    let mut out = BufWriter::new(out.lock());
    writeln!(out, "{}", ans).unwrap();
}