Polycarp analyzes the prices of the new berPhone. At his disposal are the prices for $$$n$$$ last days: $$$a_1, a_2, \dots, a_n$$$, where $$$a_i$$$ is the price of berPhone on the day $$$i$$$.Polycarp considers the price on the day $$$i$$$ to be bad if later (that is, a day with a greater number) berPhone was sold at a lower price. For example, if $$$n=6$$$ and $$$a=[3, 9, 4, 6, 7, 5]$$$, then the number of days with a bad price is $$$3$$$ — these are days $$$2$$$ ($$$a_2=9$$$), $$$4$$$ ($$$a_4=6$$$) and $$$5$$$ ($$$a_5=7$$$).Print the number of days with a bad price.You have to answer $$$t$$$ independent data sets.

int solution() {
  int t;
  scanf("%d\n", &t);
  while (t) {
    int n;
    scanf("%d\n", &n);
    int a[n];
    for (int i = 0; i < n; i++) {
      scanf("%d", &a[i]);
    }
    int c = 0;
    int min = a[n - 1];
    for (int i = n - 2; i >= 0; i--) {
      if (a[i] > min) {
        c++;
      } else {
        min = a[i];
      }
    }

    printf("%d\n", c);
    t--;
  }
}

fn solution() {
    let mut input = String::new();
    io::stdin()
        .read_line(&mut input)
        .expect("failed to read input");
    let t = input.trim().parse::<u16>().unwrap();
    for _ in 0..t {
        let mut _buffer = String::new();
        io::stdin()
            .read_line(&mut _buffer)
            .expect("failed to read input");
        let mut buffer = String::new();
        io::stdin()
            .read_line(&mut buffer)
            .expect("failed to read input");
        let a = buffer
            .split_whitespace()
            .map(|x| x.parse::<u32>().unwrap())
            .collect::<Vec<u32>>();

        let (bad, _) = a.iter().rev().fold((0, a[a.len() - 1]), |(acc, min), &x| {
            if x <= min {
                (acc, x)
            } else {
                (acc + 1, min)
            }
        });
        println!("{}", bad);
    }
}


Three swimmers decided to organize a party in the swimming pool! At noon, they started to swim from the left side of the pool.It takes the first swimmer exactly $$$a$$$ minutes to swim across the entire pool and come back, exactly $$$b$$$ minutes for the second swimmer and $$$c$$$ minutes for the third. Hence, the first swimmer will be on the left side of the pool after $$$0$$$, $$$a$$$, $$$2a$$$, $$$3a$$$, ... minutes after the start time, the second one will be at $$$0$$$, $$$b$$$, $$$2b$$$, $$$3b$$$, ... minutes, and the third one will be on the left side of the pool after $$$0$$$, $$$c$$$, $$$2c$$$, $$$3c$$$, ... minutes.You came to the left side of the pool exactly $$$p$$$ minutes after they started swimming. Determine how long you have to wait before one of the swimmers arrives at the left side of the pool.

int solution(void) {

  long long int cases;
  long long int hasila;
  long long int hasilb;
  long long int a;
  long long int b;
  long long int c;

  scanf("%lld", &cases);

  long long int hasilakhir[cases];

  for (int x = 0; x < cases; x++) {
    scanf("%lld %lld %lld %lld", &hasila, &a, &b, &c);

    if (hasila % a == 0 || hasila % b == 0 || hasila % c == 0) {
      hasilakhir[x] = 0;
    } else {
      hasilakhir[x] = a - (hasila - 1) % a - 1;
      hasilb = b - (hasila - 1) % b - 1;

      if (hasilb < hasilakhir[x]) {
        hasilakhir[x] = hasilb;
      }

      hasilb = c - (hasila - 1) % c - 1;

      if (hasilb < hasilakhir[x]) {
        hasilakhir[x] = hasilb;
      }
    }
  }
  for (int x = 0; x < cases; x++) {
    printf("%lld\n", hasilakhir[x]);
  }
}

fn solution() -> std::io::Result<()> {
    use std::io::Read;
    let mut buf = String::new();
    std::io::stdin().read_to_string(&mut buf)?;
    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 p = scan!(u128);
        let mut ans = std::u128::MAX;

        for _ in 0..3 {
            let s = scan!(u128);
            let ub = if p % s == 0 { p / s } else { p / s + 1 };
            ans = ans.min((ub * s) - p);
        }
        print!("{}", ans);
        print!("\n");
    }
    Ok(())
}


You are given $$$q$$$ queries in the following form:Given three integers $$$l_i$$$, $$$r_i$$$ and $$$d_i$$$, find minimum positive integer $$$x_i$$$ such that it is divisible by $$$d_i$$$ and it does not belong to the segment $$$[l_i, r_i]$$$.Can you answer all the queries?Recall that a number $$$x$$$ belongs to segment $$$[l, r]$$$ if $$$l \le x \le r$$$.

int solution() {
  int n;
  int i;
  int x;
  scanf("%d", &n);
  int l[n];
  int r[n];
  int d[n];
  for (i = 0; i < n; i++) {
    scanf("%d%d%d", &l[i], &r[i], &d[i]);
    if (l[i] - d[i] > 0) {
      printf("%d\n", d[i] * 1);
    } else {
      x = r[i] / d[i];
      printf("%d\n", (x + 1) * d[i]);
    }
  }
}

fn solution() {
    let n: usize = {
        let mut read_buf = String::new();
        io::stdin().read_line(&mut read_buf).unwrap();
        read_buf.trim().parse().unwrap()
    };
    for _ in 0..n {
        let (l, r, d): (usize, usize, usize) = {
            let mut read_buf = String::new();
            io::stdin().read_line(&mut read_buf).unwrap();
            let mut read_buf = read_buf.split_whitespace().map(|s| s.parse().unwrap());
            (
                read_buf.next().unwrap(),
                read_buf.next().unwrap(),
                read_buf.next().unwrap(),
            )
        };
        if d < l || d > r {
            println!("{}", d);
        } else {
            println!("{}", (r / d + 1) * d);
        }
    }
}


Score : <var>100</var> points

Problem Statement

We have two integers: <var>A</var> and <var>B</var>.

Print the largest number among <var>A + B</var>, <var>A - B</var>, and <var>A \times B</var>.

Constraints

All values in input are integers.

<var>-100 \leq A,\ B \leq 100</var>

Input

Input is given from Standard Input in the following format:

<var>A</var> <var>B</var>

Output

Print the largest number among <var>A + B</var>, <var>A - B</var>, and <var>A \times B</var>.

Sample Input 1

-13 3

Sample Output 1

-10

The largest number among <var>A + B = -10</var>, <var>A - B = -16</var>, and <var>A \times B = -39</var> is <var>-10</var>.

Sample Input 2

1 -33

Sample Output 2

34

The largest number among <var>A + B = -32</var>, <var>A - B = 34</var>, and <var>A \times B = -33</var> is <var>34</var>.

Sample Input 3

13 3

Sample Output 3

39

The largest number among <var>A + B = 16</var>, <var>A - B = 10</var>, and <var>A \times B = 39</var> is <var>39</var>.

int solution() {
  int A = 0;
  int B = 0;
  int pl = 0;
  int mn = 0;
  int mu = 0;
  int kai = 0;
  scanf("%d %d", &A, &B);
  pl = A + B;
  mn = A - B;
  mu = A * B;
  kai = pl;
  if (kai < mn) {
    kai = mn;
  }
  if (kai < mu) {
    kai = mu;
  }
  printf("%d\n", kai);

  return 0;
}

fn solution() {
    let mut s: String = String::new();
    std::io::stdin().read_to_string(&mut s).ok();
    let mut itr = s.split_whitespace();
    let a: i32 = itr.next().unwrap().parse().unwrap();
    let b: i32 = itr.next().unwrap().parse().unwrap();
    println!("{}", max(a + b, max(a - b, a * b)))
}


Monocarp is the coach of the Berland State University programming teams. He decided to compose a problemset for a training session for his teams.Monocarp has $$$n$$$ problems that none of his students have seen yet. The $$$i$$$-th problem has a topic $$$a_i$$$ (an integer from $$$1$$$ to $$$n$$$) and a difficulty $$$b_i$$$ (an integer from $$$1$$$ to $$$n$$$). All problems are different, that is, there are no two tasks that have the same topic and difficulty at the same time.Monocarp decided to select exactly $$$3$$$ problems from $$$n$$$ problems for the problemset. The problems should satisfy at least one of two conditions (possibly, both):  the topics of all three selected problems are different;  the difficulties of all three selected problems are different. Your task is to determine the number of ways to select three problems for the problemset.

int solution() {
  int t;
  scanf("%d", &t);
  while (t--) {
    int n;
    scanf("%d", &n);
    int a[200005];
    int b[200005];
    int diff[200005];
    int topic[200005];
    int i = 0;
    memset(diff, 0, (n + 1) * sizeof *diff),
        memset(topic, 0, (n + 1) * sizeof *topic);
    for (i = 1; i <= n; i++) {
      scanf("%d %d", &a[i], &b[i]);
      topic[a[i]]++;
      diff[b[i]]++;
    }
    long long int sum = 0;
    sum = (long long)n * (n - 1) * (n - 2) / 6;
    for (i = 1; i <= n; i++) {
      sum -= (long long)(topic[a[i]] - 1) * (diff[b[i]] - 1);
    }
    printf("%lld\n", sum);
  }
  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 n: usize = lines.next().unwrap().unwrap().parse().unwrap();
        let mut a: Vec<u64> = vec![0; n];
        let mut b: Vec<u64> = vec![0; n];
        let mut p: Vec<(usize, usize)> = vec![];
        for _ in 0..n {
            let x: Vec<usize> = lines
                .next()
                .unwrap()
                .unwrap()
                .split(' ')
                .map(|x| x.parse().unwrap())
                .collect();
            let i = x[0] - 1;
            let j = x[1] - 1;
            a[i] += 1;
            b[j] += 1;
            p.push((i, j));
        }
        let mut sub = 0;
        for (x, y) in p {
            sub += (a[x] - 1) * (b[y] - 1);
        }
        let ans = n as u64 * (n as u64 - 1) * (n as u64 - 2) / 6 - sub;
        println!("{}", ans);
    }
}


Bachgold problem is very easy to formulate. Given a positive integer n represent it as a sum of maximum possible number of prime numbers. One can prove that such representation exists for any integer greater than 1.Recall that integer k is called prime if it is greater than 1 and has exactly two positive integer divisors — 1 and k.

fn solution() {
    let mut val = String::new();
    io::stdin()
        .read_line(&mut val)
        .expect("Не удалось прочитать строку");

    let mut val: u32 = val.trim().parse().expect("Пожалуйста, введите число!");

    if val.is_multiple_of(2) {
        println!("{}", val / 2);
        for _ in 0..val / 2 {
            print!("2 ");
        }
    } else {
        val -= 3;
        println!("{}", val / 2 + 1);
        for _ in 0..val / 2 {
            print!("2 ");
        }
        print!("3");
    }
}


Score : <var>400</var> points

Problem Statement

Dolphin is planning to generate a small amount of a certain chemical substance C.

In order to generate the substance C, he must prepare a solution which is a mixture of two substances A and B in the ratio of <var>M_a:M_b</var>.

He does not have any stock of chemicals, however, so he will purchase some chemicals at a local pharmacy.

The pharmacy sells <var>N</var> kinds of chemicals. For each kind of chemical, there is exactly one package of that chemical in stock.

The package of chemical <var>i</var> contains <var>a_i</var> grams of the substance A and <var>b_i</var> grams of the substance B, and is sold for <var>c_i</var> yen (the currency of Japan).

Dolphin will purchase some of these packages. For some reason, he must use all contents of the purchased packages to generate the substance C.

Find the minimum amount of money required to generate the substance C.

If it is not possible to generate the substance C by purchasing any combination of packages at the pharmacy, report that fact.

Constraints

<var>1≦N≦40</var>

<var>1≦a_i,b_i≦10</var>

<var>1≦c_i≦100</var>

<var>1≦M_a,M_b≦10</var>

<var>gcd(M_a,M_b)=1</var>

<var>a_i</var>, <var>b_i</var>, <var>c_i</var>, <var>M_a</var> and <var>M_b</var> are integers.

Input

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

<var>N</var> <var>M_a</var> <var>M_b</var>
<var>a_1</var> <var>b_1</var> <var>c_1</var>
<var>a_2</var> <var>b_2</var> <var>c_2</var>
<var>:</var>
<var>a_N</var> <var>b_N</var> <var>c_N</var>

Output

Print the minimum amount of money required to generate the substance C. If it is not possible to generate the substance C, print -1 instead.

Sample Input 1

3 1 1
1 2 1
2 1 2
3 3 10

Sample Output 1

3

The amount of money spent will be minimized by purchasing the packages of chemicals <var>1</var> and <var>2</var>.

In this case, the mixture of the purchased chemicals will contain <var>3</var> grams of the substance A and <var>3</var> grams of the substance B, which are in the desired ratio: <var>3:3=1:1</var>.

The total price of these packages is <var>3</var> yen.

Sample Input 2

1 1 10
10 10 10

Sample Output 2

-1

The ratio <var>1:10</var> of the two substances A and B cannot be satisfied by purchasing any combination of the packages. Thus, the output should be -1.

fn solution() {
    input! {
        n: usize,
        ma: usize,
        mb: usize,
        v: [(usize, usize, usize); n]
    }
    let max_c = 100000;
    let nab = n * 10 + 1;
    let mut dp = vec![vec![vec![max_c; nab]; nab]; n + 1];
    dp[0][0][0] = 0;
    for i in 0..n {
        dp[i + 1] = dp[i].clone();
        for j in v[i].0..nab {
            for k in v[i].1..nab {
                dp[i + 1][j][k] = min(dp[i + 1][j][k], dp[i][j - v[i].0][k - v[i].1] + v[i].2);
            }
        }
    }
    let mut ans = max_c;
    let mut a = ma;
    let mut b = mb;
    while a < nab && b < nab {
        ans = min(ans, dp[n][a][b]);
        a += ma;
        b += mb;
    }
    if ans == max_c {
        println!("-1");
    } else {
        println!("{}", ans);
    }
}


Score: <var>200</var> points

Problem Statement

Takahashi will do a tap dance. The dance is described by a string <var>S</var> where each character is L, R, U, or D. These characters indicate the positions on which Takahashi should step. He will follow these instructions one by one in order, starting with the first character.

<var>S</var> is said to be easily playable if and only if it satisfies both of the following conditions:

Every character in an odd position (<var>1</var>-st, <var>3</var>-rd, <var>5</var>-th, <var>\ldots</var>) is R, U, or D.

Every character in an even position (<var>2</var>-nd, <var>4</var>-th, <var>6</var>-th, <var>\ldots</var>) is L, U, or D.

Your task is to print Yes if <var>S</var> is easily playable, and No otherwise.

Constraints

<var>S</var> is a string of length between <var>1</var> and <var>100</var> (inclusive).

Each character of <var>S</var> is L, R, U, or D.

Input

Input is given from Standard Input in the following format:

<var>S</var>

Output

Print Yes if <var>S</var> is easily playable, and No otherwise.

Sample Input 1

RUDLUDR

Sample Output 1

Yes

Every character in an odd position (<var>1</var>-st, <var>3</var>-rd, <var>5</var>-th, <var>7</var>-th) is R, U, or D.

Every character in an even position (<var>2</var>-nd, <var>4</var>-th, <var>6</var>-th) is L, U, or D.

Thus, <var>S</var> is easily playable.

Sample Input 2

DULL

Sample Output 2

No

The <var>3</var>-rd character is not R, U, nor D, so <var>S</var> is not easily playable.

Sample Input 3

UUUUUUUUUUUUUUU

Sample Output 3

Yes

Sample Input 4

ULURU

Sample Output 4

No

Sample Input 5

RDULULDURURLRDULRLR

Sample Output 5

Yes

fn solution() {
    let mut buf = String::new();
    std::io::stdin().read_line(&mut buf).ok();
    if buf
        .chars()
        .enumerate()
        .all(|(i, c)| i % 2 == 1 && c != 'R' || i % 2 == 0 && c != 'L')
    {
        println!("Yes");
    } else {
        println!("No");
    };
}


Score : <var>100</var> points

Problem Statement

Given an integer <var>N</var> not less than <var>3</var>, find the sum of the interior angles of a regular polygon with <var>N</var> sides.

Print the answer in degrees, but do not print units.

Constraints

<var>3 \leq N \leq 100</var>

Input

Input is given from Standard Input in the following format:

<var>N</var>

Output

Print an integer representing the sum of the interior angles of a regular polygon with <var>N</var> sides.

Sample Input 1

3

Sample Output 1

180

The sum of the interior angles of a regular triangle is <var>180</var> degrees.

Sample Input 2

100

Sample Output 2

17640

int solution(void) {
  int N = 0;
  int ans = 0;
  scanf("%d", &N);
  ans = 180 + (N - 3) * 180;
  printf("%d\n", ans);
  return 0;
}

fn solution() {
    let mut buf = String::new();
    std::io::stdin().read_line(&mut buf).unwrap();
    let mut iter = buf.split_whitespace();
    let n: usize = iter.next().unwrap().parse().unwrap();

    println!("{}", (n - 2) * 180);
}


You are given string s. Your task is to determine if the given string s contains two non-overlapping substrings "AB" and "BA" (the substrings can go in any order).

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

    io::stdin().read_line(&mut s).unwrap();

    let s = s.trim();

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

    let mut fin: usize = s.len();
    let mut found = false;

    for i in 0..s.len() - 1_usize {
        if s[i] == 'A' && s[i + 1] == 'B' {
            fin = i + 2;
            break;
        }
    }

    for i in fin..s.len() - 1_usize {
        if s[i] == 'B' && s[i + 1] == 'A' {
            found = true;
        }
    }

    fin = s.len();

    for i in 0..s.len() - 1_usize {
        if s[i] == 'B' && s[i + 1] == 'A' {
            fin = i + 2;
            break;
        }
    }

    for i in fin..s.len() - 1_usize {
        if s[i] == 'A' && s[i + 1] == 'B' {
            found = true;
        }
    }

    if found {
        println!("YES");
    } else {
        println!("NO");
    }
}


NN is an experienced internet user and that means he spends a lot of time on the social media. Once he found the following image on the Net, which asked him to compare the sizes of inner circles:  It turned out that the circles are equal. NN was very surprised by this fact, so he decided to create a similar picture himself.He managed to calculate the number of outer circles $$$n$$$ and the radius of the inner circle $$$r$$$. NN thinks that, using this information, you can exactly determine the radius of the outer circles $$$R$$$ so that the inner circle touches all of the outer ones externally and each pair of neighboring outer circles also touches each other. While NN tried very hard to guess the required radius, he didn't manage to do that. Help NN find the required radius for building the required picture.

int solution() {
  float n;
  float r;
  scanf("%f %f", &n, &r);

  float val = 360 / (2 * n);

  float theta = (3.14159265 / 180) * val;
  float ans = (r * sinf(theta)) / (1 - sinf(theta));
  printf("%f\n", ans);
  return 0;
}

fn solution() {
    let mut input = String::new();
    std::io::stdin().read_line(&mut input).expect("Input error");
    let input_segment: Vec<f64> = input
        .split_whitespace()
        .map(|x| x.parse().expect("Not an integer"))
        .collect();
    let a: f64 = input_segment[0];
    let b: f64 = input_segment[1];
    let angle: f64 = (a - 2.0) / (2.0 * a) * 180.0;
    let outer_radius: f64 = angle.to_radians().cos() / (1.0 - angle.to_radians().cos());
    println!("{:.7}", outer_radius * b);
}


Score : <var>100</var> points

Problem Statement

You are given three words <var>s_1</var>, <var>s_2</var> and <var>s_3</var>, each composed of lowercase English letters, with spaces in between.
Print the acronym formed from the uppercased initial letters of the words.

Constraints

<var>s_1</var>, <var>s_2</var> and <var>s_3</var> are composed of lowercase English letters.

<var>1 ≤ |s_i| ≤ 10 (1≤i≤3)</var>

Input

Input is given from Standard Input in the following format:

<var>s_1</var> <var>s_2</var> <var>s_3</var>

Output

Print the answer.

Sample Input 1

atcoder beginner contest

Sample Output 1

ABC

The initial letters of atcoder, beginner and contest are a, b and c. Uppercase and concatenate them to obtain ABC.

Sample Input 2

resident register number

Sample Output 2

RRN

Sample Input 3

k nearest neighbor

Sample Output 3

KNN

Sample Input 4

async layered coding

Sample Output 4

ALC

fn solution() {
    let mut buf = String::new();
    io::stdin().read_line(&mut buf).expect("");
    let iter = buf.split_whitespace();
    let mut v: Vec<Vec<char>> = Vec::new();
    for i in iter {
        v.push(i.chars().collect::<Vec<char>>());
    }
    let s1 = v[0].clone();
    let s1c = s1[0].to_uppercase().collect::<Vec<char>>();
    let s2 = v[1].clone();
    let s2c = s2[0].to_uppercase().collect::<Vec<char>>();
    let s3 = v[2].clone();
    let s3c = s3[0].to_uppercase().collect::<Vec<char>>();
    let s = format!("{}{}{}", s1c[0], s2c[0], s3c[0]);
    println!("{}", s);
}


Nick's company employed n people. Now Nick needs to build a tree hierarchy of «supervisor-surbodinate» relations in the company (this is to say that each employee, except one, has exactly one supervisor). There are m applications written in the following form: «employee ai is ready to become a supervisor of employee bi at extra cost ci». The qualification qj of each employee is known, and for each application the following is true: qai > qbi. Would you help Nick calculate the minimum cost of such a hierarchy, or find out that it is impossible to build it.

int solution() {
  int i;
  int j;
  int n;
  int m;
  int *q;
  int a;
  int b;
  int c;
  int zero;
  int sum;
  struct worker {
    int *c, cnt;
  } *w;

  scanf("%d", &n);
  q = malloc(sizeof(*q) * n);
  w = malloc(sizeof(*w) * n);
  for (i = 0; i < n; i++) {
    scanf("%d", &q[i]);
  }
  scanf("%d", &m);
  for (i = 0; i < n; i++) {
    w[i].c = malloc(sizeof(*w[i].c) * m);
    w[i].cnt = 0;
  }
  for (i = 0; i < m; i++) {
    scanf("%d%d%d", &a, &b, &c);
    w[b - 1].c[w[b - 1].cnt++] = c;
  }

  zero = sum = 0;
  for (i = 0; i < n; i++) {
    if (w[i].cnt == 0) {
      if (zero) {
        printf("-1\n");
        return 0;
      }
      zero = 1;
    }
  }
  for (i = 0; i < n; i++) {
    int min = 1000001;

    if (w[i].cnt != 0) {
      for (j = 0; j < w[i].cnt; j++) {
        if (min > w[i].c[j]) {
          min = w[i].c[j];
        }
      }
      sum += min;
    }
  }
  printf("%d\n", sum);
  return 0;
}

fn solution() -> io::Result<()> {
    let stdin = io::stdin();
    let mut input = stdin.lock();
    let mut buffer = String::new();
    input.read_line(&mut buffer)?;
    let n: usize = buffer.trim().parse().unwrap();

    buffer.clear();
    input.read_line(&mut buffer)?;
    let mut max_qual: i32 = -1;
    let mut max_qual_count: u32 = 0;

    for str_qual in buffer.split_ascii_whitespace() {
        let qual: i32 = str_qual.parse().unwrap();
        if qual > max_qual {
            max_qual = qual;
            max_qual_count = 1;
        } else if qual == max_qual {
            max_qual_count += 1;
        }
    }
    if max_qual_count >= 2 {
        println!("-1");
        return Ok(());
    }
    buffer.clear();
    input.read_line(&mut buffer)?;
    let m: usize = buffer.trim().parse().unwrap();
    let mut cheapest_boss: Vec<Option<u32>> = vec![None; n];
    for edge_line in input.lines().take(m) {
        let edge_spec: Vec<u32> = edge_line?
            .split_ascii_whitespace()
            .map(|x| x.parse().unwrap())
            .collect();
        let _a = edge_spec[0] - 1;
        let b = (edge_spec[1] - 1) as usize;
        let c = edge_spec[2];
        let curr_cheapest = cheapest_boss[b];
        if c < curr_cheapest.unwrap_or(c + 1) {
            cheapest_boss[b] = Some(c);
        }
    }
    let mut total: u64 = 0;
    let mut bossless: u32 = 0;
    for cheapest in cheapest_boss.iter() {
        match cheapest {
            None => {
                bossless += 1;
            }
            Some(cheapest) => {
                total += *cheapest as u64;
            }
        }
    }
    if bossless <= 1 {
        println!("{}", total);
    } else {
        println!("-1");
    }
    Ok(())
}


Score : <var>500</var> points

Problem Statement

There is an apple tree that bears apples of <var>N</var> colors. The <var>N</var> colors of these apples are numbered <var>1</var> to <var>N</var>, and there are <var>a_i</var> apples of Color <var>i</var>.

You and Lunlun the dachshund alternately perform the following operation (starting from you):

Choose one or more apples from the tree and eat them. Here, the apples chosen at the same time must all have different colors.

The one who eats the last apple from the tree will be declared winner. If both you and Lunlun play optimally, which will win?

Constraints

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

<var>1 \leq a_i \leq 10^9</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

If you will win, print first; if Lunlun will win, print second.

Sample Input 1

2
1
2

Sample Output 1

first

Let Color <var>1</var> be red, and Color <var>2</var> be blue. In this case, the tree bears one red apple and two blue apples.

You should eat the red apple in your first turn. Lunlun is then forced to eat one of the blue apples, and you can win by eating the other in your next turn.

Note that you are also allowed to eat two apples in your first turn, one red and one blue (not a winning move, though).

Sample Input 2

3
100000
30000
20000

Sample Output 2

second

int solution() {
  int n;
  int check = 0;
  int s[100040];
  scanf("%d", &n);

  for (int i = 0; i < n; i++) {
    scanf("%d", &s[i]);
    if (s[i] % 2 == 1) {
      check++;
    }
  }
  if (check >= 1) {
    printf("first");
  } else {
    printf("second");
  }
}

fn solution() {
    input! {
        n: usize,
        a: [usize; n],
    }
    if a.iter().any(|n| n % 2 != 0) {
        println!("first");
    } else {
        println!("second");
    }
}


A guy named Vasya attends the final grade of a high school. One day Vasya decided to watch a match of his favorite hockey team. And, as the boy loves hockey very much, even more than physics, he forgot to do the homework. Specifically, he forgot to complete his physics tasks. Next day the teacher got very angry at Vasya and decided to teach him a lesson. He gave the lazy student a seemingly easy task: You are given an idle body in space and the forces that affect it. The body can be considered as a material point with coordinates (0; 0; 0). Vasya had only to answer whether it is in equilibrium. "Piece of cake" — thought Vasya, we need only to check if the sum of all vectors is equal to 0. So, Vasya began to solve the problem. But later it turned out that there can be lots and lots of these forces, and Vasya can not cope without your help. Help him. Write a program that determines whether a body is idle or is moving by the given vectors of forces.

int solution() {
  int A[500][500] = {0};
  int sum = 0;

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

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

  for (int j = 0; j < 3; j++) {
    sum = 0;
    for (int i = 0; i < n; i++) {
      sum = sum + A[i][j];
    }

    if (sum != 0) {
      printf("NO");
      return 0;
    }
  }

  printf("YES");
  return 0;
}

fn solution() {
    let mut result: Vec<i32> = vec![0, 0, 0];
    std::io::stdin().lock().lines().skip(1).for_each(|l| {
        let v: Vec<i32> = l
            .expect("stdin not work")
            .trim()
            .split(' ')
            .map(|s| s.parse::<i32>().expect("not number"))
            .collect();
        result[0] += v[0];
        result[1] += v[1];
        result[2] += v[2];
    });
    println!(
        "{}",
        if result[0] == 0 && result[1] == 0 && result[2] == 0 {
            "YES"
        } else {
            "NO"
        }
    );
}


You are given an array $$$a$$$ of $$$2n$$$ distinct integers. You want to arrange the elements of the array in a circle such that no element is equal to the the arithmetic mean of its $$$2$$$ neighbours.More formally, find an array $$$b$$$, such that: $$$b$$$ is a permutation of $$$a$$$.For every $$$i$$$ from $$$1$$$ to $$$2n$$$, $$$b_i \neq \frac{b_{i-1}+b_{i+1}}{2}$$$, where $$$b_0 = b_{2n}$$$ and $$$b_{2n+1} = b_1$$$. It can be proved that under the constraints of this problem, such array $$$b$$$ always exists.

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 t = lines.next().unwrap().parse::<usize>().unwrap();

    for _ in 0..t {
        let n = lines.next().unwrap().parse::<usize>().unwrap();

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

        let mut a = it.collect::<Vec<_>>();
        a.sort();

        for i in 0..n {
            write!(&mut output, "{} {} ", a[i], a[2 * n - 1 - i]).unwrap();
        }
        writeln!(&mut output).unwrap();
    }
}


Score : <var>300</var> points

Problem Statement

You are given an integer <var>N</var>.

Find a triple of positive integers <var>h</var>, <var>n</var> and <var>w</var> such that <var>4/N = 1/h + 1/n + 1/w</var>.

If there are multiple solutions, any of them will be accepted.

Constraints

It is guaranteed that, for the given integer <var>N</var>, there exists a solution such that <var>h,n,w \leq 3500</var>.

Inputs

Input is given from Standard Input in the following format:

<var>N</var>

Outputs

Print a triple of positive integers <var>h</var>, <var>n</var> and <var>w</var> that satisfies the condition, in the following format:

<var>h</var> <var>n</var> <var>w</var>

Sample Input 1

2

Sample Output 1

1 2 2

<var>4/2 = 1/1 + 1/2 + 1/2</var>.

Sample Input 2

3485

Sample Output 2

872 1012974 1539173474040

It is allowed to use an integer exceeding <var>3500</var> in a solution.

Sample Input 3

4664

Sample Output 3

3498 3498 3498

int solution() {
  long long n;
  scanf("%lld", &n);
  if (n % 2 == 0) {
    printf("%lld %lld %lld", n, n, n / 2);
    return 0;
  }
  for (long long i = 1; i <= 3500; i++) {
    for (long long j = 1; j <= 3500; j++) {
      if ((4 * i * j - n * i - n * j) > 0 &&
          n * i * j % (4 * i * j - n * i - n * j) == 0 &&
          n * i * j / (4 * i * j - n * i - n * j) > 0) {
        printf("%lld %lld %lld", i, j, n * i * j / (4 * i * j - n * i - n * j));
        return 0;
      }
    }
  }

  return 0;
}

fn solution() {
    let mut buf = String::new();
    std::io::stdin().read_to_string(&mut buf).unwrap();
    let N: i64 = buf.trim().parse().unwrap();
    for a in 1..3500 {
        for b in a..3500 {
            let div = 4 * a * b - N * b - N * a;
            if div <= 0 {
                continue;
            }

            let m = (N * a * b) % (4 * a * b - N * b - N * a);
            let c = (N * a * b) / (4 * a * b - N * b - N * a);
            if m > 0 || c <= 0 {
                continue;
            }

            println!("{} {} {}", a, b, c);
            return;
        }
    }
}


It is raining heavily. But this is the first day for Serval, who just became 3 years old, to go to the kindergarten. Unfortunately, he lives far from kindergarten, and his father is too busy to drive him there. The only choice for this poor little boy is to wait for a bus on this rainy day. Under such circumstances, the poor boy will use the first bus he sees no matter where it goes. If several buses come at the same time, he will choose one randomly.Serval will go to the bus station at time $$$t$$$, and there are $$$n$$$ bus routes which stop at this station. For the $$$i$$$-th bus route, the first bus arrives at time $$$s_i$$$ minutes, and each bus of this route comes $$$d_i$$$ minutes later than the previous one.As Serval's best friend, you wonder which bus route will he get on. If several buses arrive at the same time, you can print any of them.

int solution() {
  int n;
  int t;
  scanf("%d", &n);
  scanf("%d", &t);
  int s[n + 2];
  int d[n + 2];
  for (int i = 0; i < n; i++) {
    scanf("%d", &s[i]);
    scanf("%d", &d[i]);
  }
  int ans = 0;

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

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

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!();
    let t = get!();
    let mut xs = vec![];
    for i in 0..n {
        let mut s = get!();
        let d = get!();
        if s < t {
            let e = t - s;
            s += ((e + d - 1) / d) * d;
        }
        xs.push((s, i + 1));
    }
    xs.sort();
    println!("{}", xs[0].1);
}


You are given an integer $$$n$$$.Let's define $$$s(n)$$$ as the string "BAN" concatenated $$$n$$$ times. For example, $$$s(1)$$$ = "BAN", $$$s(3)$$$ = "BANBANBAN". Note that the length of the string $$$s(n)$$$ is equal to $$$3n$$$.Consider $$$s(n)$$$. You can perform the following operation on $$$s(n)$$$ any number of times (possibly zero): Select any two distinct indices $$$i$$$ and $$$j$$$ $$$(1 \leq i, j \leq 3n, i \ne j)$$$. Then, swap $$$s(n)_i$$$ and $$$s(n)_j$$$. You want the string "BAN" to not appear in $$$s(n)$$$ as a subsequence. What's the smallest number of operations you have to do to achieve this? Also, find one such shortest sequence of operations.A string $$$a$$$ is a subsequence of a string $$$b$$$ if $$$a$$$ can be obtained from $$$b$$$ by deletion of several (possibly, zero or all) characters.

fn solution() {
    let mut buf = BufWriter::new(io::stdout().lock());
    io::stdin()
        .lines()
        .skip(1)
        .flatten()
        .flat_map(|s| s.parse::<isize>())
        .for_each(|n| {
            let m = n.wrapping_add(1).wrapping_shr(1);
            writeln!(buf, "{m}").ok();
            let mut i = 1isize;
            let mut j = n.wrapping_mul(3);
            while i < j {
                writeln!(buf, "{i} {j}").ok();
                j = j.wrapping_sub(3);
                i = i.wrapping_add(3);
            }
        });
}


Score : <var>500</var> points

Problem Statement

We have a deck consisting of <var>N</var> cards. Each card has an integer written on it. The integer on the <var>i</var>-th card from the top is <var>a_i</var>.

Two people X and Y will play a game using this deck. Initially, X has a card with <var>Z</var> written on it in his hand, and Y has a card with <var>W</var> written on it in his hand. Then, starting from X, they will alternately perform the following action:

Draw some number of cards from the top of the deck. Then, discard the card in his hand and keep the last drawn card instead. Here, at least one card must be drawn.

The game ends when there is no more card in the deck. The score of the game is the absolute difference of the integers written on the cards in the two players' hand.

X will play the game so that the score will be maximized, and Y will play the game so that the score will be minimized. What will be the score of the game?

Constraints

All input values are integers.

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

<var>1 \leq Z, W, a_i \leq 10^9</var>

Input

Input is given from Standard Input in the following format:

<var>N</var> <var>Z</var> <var>W</var>
<var>a_1</var> <var>a_2</var> <var>...</var> <var>a_N</var>

Output

Print the score.

Sample Input 1

3 100 100
10 1000 100

Sample Output 1

900

If X draws two cards first, Y will draw the last card, and the score will be <var>|1000 - 100| = 900</var>.

Sample Input 2

3 100 1000
10 100 100

Sample Output 2

900

If X draws all the cards first, the score will be <var>|1000 - 100| = 900</var>.

Sample Input 3

5 1 1
1 1 1 1 1

Sample Output 3

0

Sample Input 4

1 1 1
1000000000

Sample Output 4

999999999

int solution(void) {

  long n;
  long z;
  long w;
  scanf("%ld %ld %ld", &n, &z, &w);
  long a[n];
  for (long i = 0; i < n; i++) {
    scanf("%ld", &a[i]);
  }
  long score = labs(a[n - 1] - w);
  if (n != 1) {
    if (labs(a[n - 1] - a[n - 2]) > score) {
      score = labs(a[n - 1] - a[n - 2]);
    }
  }
  printf("%ld\n", score);

  return 0;
}

fn solution() {
    let mut buf = String::new();
    io::stdin().read_line(&mut buf).unwrap();
    let s: Vec<&str> = buf.split_whitespace().collect();
    let n: usize = s[0].parse().unwrap();
    let w: i32 = s[2].parse().unwrap();

    let mut buf2 = String::new();
    io::stdin().read_line(&mut buf2).unwrap();
    let s2: Vec<&str> = buf2.split_whitespace().collect();
    let mut last1: i32 = 0;
    if n > 1 {
        last1 = s2[n - 2].parse().unwrap();
    }
    let last0: i32 = s2[n - 1].parse().unwrap();
    let choice0 = (last0 - w).abs();
    let mut choice1 = 0;
    if n > 1 {
        choice1 = (last1 - last0).abs();
    }
    println!("{}", cmp::max(choice0, choice1));
}


You have three piles of candies: red, green and blue candies:  the first pile contains only red candies and there are $$$r$$$ candies in it,  the second pile contains only green candies and there are $$$g$$$ candies in it,  the third pile contains only blue candies and there are $$$b$$$ candies in it. Each day Tanya eats exactly two candies of different colors. She is free to choose the colors of eaten candies: the only restriction that she can't eat two candies of the same color in a day.Find the maximal number of days Tanya can eat candies? Each day she needs to eat exactly two candies.

int solution() {
  unsigned long r;
  unsigned long g;
  unsigned long b;
  short t;
  scanf("%hd", &t);
  unsigned long *max1;
  unsigned long *max2;
  unsigned long *max3;
  while (t--) {
    scanf("%lu %lu %lu", &r, &g, &b);
    if (r >= g && g >= b) {
      max1 = &r;
      max2 = &g;
      max3 = &b;
    } else if (r >= b && b >= g) {
      max1 = &r;
      max2 = &b;
      max3 = &g;
    } else if (g >= r && r >= b) {
      max1 = &g;
      max2 = &r;
      max3 = &b;
    } else if (g >= b && b >= r) {
      max1 = &g;
      max2 = &b;
      max3 = &r;
    } else if (b >= r && r >= g) {
      max1 = &b;
      max2 = &r;
      max3 = &g;
    } else {
      max1 = &b;
      max2 = &g;
      max3 = &r;
    }
    if (*max1 >= (*max2 + *max3)) {
      printf("%lu\n", *max2 + *max3);
    } else {
      printf("%lu\n", (*max1 + *max2 + *max3) / 2);
    }
  }
  return 0;
}

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 t = get!();

    for _ in 0..t {
        let r = get!();
        let g = get!();
        let b = get!();
        let mut vs = [r, g, b];
        vs.sort();
        let max = vs[2];
        let sum = vs[0..2].iter().sum();
        let min = vs[0];
        if max >= sum {
            println!("{}", sum);
        } else {
            let diff = sum - max;
            let p = std::cmp::min(min * 2, diff);
            let ans = max + p / 2;
            println!("{}", ans);
        }
    }
}


Score : <var>100</var> points

Problem Statement

You are given two integers <var>A</var> and <var>B</var> as the input. Output the value of <var>A + B</var>.

However, if <var>A + B</var> is <var>10</var> or greater, output error instead.

Constraints

<var>A</var> and <var>B</var> are integers.

<var>1 ≤ A, B ≤ 9</var>

Input

Input is given from Standard Input in the following format:

<var>A</var> <var>B</var>

Output

If <var>A + B</var> is <var>10</var> or greater, print the string error (case-sensitive); otherwise, print the value of <var>A + B</var>.

Sample Input 1

6 3

Sample Output 1

9

Sample Input 2

6 4

Sample Output 2

error

int solution() {
  int a = 0;
  int b = 0;
  scanf("%d%d", &a, &b);
  if (a + b >= 10) {
    printf("error\n");
  } else {
    printf("%d\n", a + b);
  }
  return 0;
}

fn solution() {
    let stdin = io::stdin();
    let mut buf = String::new();
    stdin.read_line(&mut buf).ok();

    let mut it = buf.split_whitespace().map(|n| i64::from_str(n).unwrap());
    let (a, b) = (it.next().unwrap(), it.next().unwrap());

    if a + b >= 10 {
        println!("error");
        return;
    }
    println!("{}", a + b);
}


Score : <var>200</var> points

Problem Statement

We have held a popularity poll for <var>N</var> items on sale. Item <var>i</var> received <var>A_i</var> votes.

From these <var>N</var> items, we will select <var>M</var> as popular items. However, we cannot select an item with less than <var>\dfrac{1}{4M}</var> of the total number of votes.

If <var>M</var> popular items can be selected, print Yes; otherwise, print No.

Constraints

<var>1 \leq M \leq N \leq 100</var>

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

<var>A_i</var> are distinct.

All values in input are integers.

Input

Input is given from Standard Input in the following format:

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

Output

If <var>M</var> popular items can be selected, print Yes; otherwise, print No.

Sample Input 1

4 1
5 4 2 1

Sample Output 1

Yes

There were <var>12</var> votes in total. The most popular item received <var>5</var> votes, and we can select it.

Sample Input 2

3 2
380 19 1

Sample Output 2

No

There were <var>400</var> votes in total. The second and third most popular items received less than <var>\dfrac{1}{4\times 2}</var> of the total number of votes, so we cannot select them. Thus, we cannot select two popular items.

Sample Input 3

12 3
4 56 78 901 2 345 67 890 123 45 6 789

Sample Output 3

Yes

int solution() {
  int lineup[100];
  int M = 0;
  int N = 0;
  int i = 0;
  int j = 0;
  int k = 0;
  int add = 0;

  scanf("%d %d", &N, &M);
  for (i = 0; i < N; i++) {
    scanf("%d", &lineup[i]);
    add += lineup[i];
  }
  for (j = 0; j < N; j++) {
    if (lineup[j] * 4 * M >= add) {
      k++;
    }
  }
  if (k >= M) {
    printf("Yes");
  } else {
    printf("No");
  }
  return 0;
}

fn solution() {
    let mut s = String::new();
    stdin().read_line(&mut s).unwrap();
    let (_n, m) = {
        let mut sp = s.split_whitespace().map(|x| x.parse::<usize>().unwrap());
        (sp.next().unwrap(), sp.next().unwrap())
    };
    let mut s = String::new();
    stdin().read_line(&mut s).unwrap();
    let a: Vec<_> = s
        .split_whitespace()
        .map(|x| x.parse::<usize>().unwrap())
        .collect();

    let sum: usize = a.iter().sum();
    let line = (sum - 1) / (4 * m) + 1;
    let ok = a.iter().filter(|&&x| x >= line).count();

    if ok >= m {
        println!("Yes");
    } else {
        println!("No");
    }
}


Reverse Polish notation is a notation where every operator follows all of its operands. For example, an expression (1+2)*(5+4) in the conventional Polish notation can be represented as 1 2 + 5 4 + * in the Reverse Polish notation. One of advantages of the Reverse Polish notation is that it is parenthesis-free.

Write a program which reads an expression in the Reverse Polish notation and prints the computational result.

An expression in the Reverse Polish notation is calculated using a stack. To evaluate the expression, the program should read symbols in order. If the symbol is an operand, the corresponding value should be pushed into the stack. On the other hand, if the symbols is an operator, the program should pop two elements from the stack, perform the corresponding operations, then push the result in to the stack. The program should repeat this operations.

Input

An expression is given in a line. Two consequtive symbols (operand or operator) are separated by a space character.

You can assume that +, - and * are given as the operator and an operand is a positive integer less than 106

Output

Print the computational result in a line.

Constraints

2 ≤ the number of operands in the expression ≤ 100

1 ≤ the number of operators in the expression ≤ 99

-1 × 109 ≤ values in the stack ≤ 109

Sample Input 1

1 2 +

Sample Output 1

3

Sample Input 2

1 2 + 3 4 - *

Sample Output 2

-3

Notes

Template in C

int solution(void) {
  int_fast32_t stack[100];
  int_fast8_t index = 0;
  char element[8];

  scanf("%" SCNdFAST32, &stack[index]);
  ++index;
  scanf("%" SCNdFAST32, &stack[index]);
  ++index;

  while (EOF != scanf(" %s", element)) {
    if ('+' == element[0]) {
      --index;
      stack[index - 1] += stack[index];
    } else if ('-' == element[0]) {
      --index;
      stack[index - 1] -= stack[index];
    } else if ('*' == element[0]) {
      --index;
      stack[index - 1] *= stack[index];
    } else {
      sscanf(element, "%" SCNdFAST32, &stack[index]);
      ++index;
    }
  }

  printf("%" PRIdFAST32 "\n", stack[0]);

  return 0;
}

fn solution() {
    let stdin = stdin();
    let mut buf = String::new();
    stdin.read_line(&mut buf).ok();
    let buf = buf.trim();
    let chars: Vec<&str> = buf.split(' ').collect();
    let mut arr: Vec<i64> = Vec::new();
    for i in chars {
        match i {
            "+" | "-" | "*" => {
                let right = arr.pop().unwrap();
                let left = arr.pop().unwrap();
                let r = match i {
                    "+" => left + right,
                    "-" => left - right,
                    "*" => left * right,
                    _ => panic!(),
                };
                arr.push(r);
            }
            _ => arr.push(i.parse::<i64>().unwrap()),
        }
    }
    println!("{}", arr[0]);
}


You are given an array $$$a_1, a_2, \dots, a_n$$$. You can perform operations on the array. In each operation you can choose an integer $$$i$$$ ($$$1 \le i < n$$$), and swap elements $$$a_i$$$ and $$$a_{i+1}$$$ of the array, if $$$a_i + a_{i+1}$$$ is odd.Determine whether it can be sorted in non-decreasing order using this operation any number of times.

int solution() {

  int t = 0;
  int n = 0;
  int flag1 = 0;
  int flag2 = 0;
  int j = 0;
  int k = 0;
  long long int arr[100000];
  long long int arr1[100000];
  long long int arr2[100000];
  scanf("%d", &t);

  while (t--) {

    flag1 = 0;
    flag2 = 0;
    k = 0;
    j = 0;

    scanf("%d", &n);

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

      scanf("%lld", &arr[i]);

      if (arr[i] % 2 == 0) {

        arr1[j++] = arr[i];
      } else {

        arr2[k++] = arr[i];
      }
    }

    for (int i = 0; i < j - 1; i++) {

      if (arr1[i] > arr1[i + 1]) {

        flag1 = 1;
        break;
      }
    }

    for (int i = 0; i < k - 1; i++) {

      if (arr2[i] > arr2[i + 1]) {

        flag2 = 1;
        break;
      }
    }

    if (flag1 == 0 && flag2 == 0) {

      printf("YES\n");
    } else {

      printf("NO\n");
    }
  }

  return 0;
}

fn solution() {
    let k = std::io::stdin()
        .lock()
        .lines()
        .next()
        .unwrap()
        .unwrap()
        .parse::<i64>()
        .unwrap();
    'jester: for _ in 0..k {
        let _l = std::io::stdin()
            .lock()
            .lines()
            .next()
            .unwrap()
            .unwrap()
            .parse::<i64>()
            .unwrap();
        let mut m = String::new();
        std::io::stdin().read_line(&mut m).unwrap();
        let m = m
            .trim()
            .split(" ")
            .flat_map(str::parse::<i64>)
            .collect::<Vec<_>>();
        let mut odds = 0;
        let mut evens = 0;
        'clown: for els in m.iter().enumerate() {
            if els.1 % 2 == 0 {
                if *els.1 >= evens {
                    evens = *els.1;
                } else {
                    println!("No");
                    continue 'jester;
                }
            }
            if els.1 % 2 != 0 {
                if *els.1 >= odds {
                    odds = *els.1
                } else {
                    println!("No");
                    continue 'jester;
                }
            }
        }
        println!("Yes");
    }
}


Score: <var>200</var> points

Problem Statement

A country decides to build a palace.

In this country, the average temperature of a point at an elevation of <var>x</var> meters is <var>T-x \times 0.006</var> degrees Celsius.

There are <var>N</var> places proposed for the place. The elevation of Place <var>i</var> is <var>H_i</var> meters.

Among them, Princess Joisino orders you to select the place whose average temperature is the closest to <var>A</var> degrees Celsius, and build the palace there.

Print the index of the place where the palace should be built.

It is guaranteed that the solution is unique.

Constraints

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

<var>0 \leq T \leq 50</var>

<var>-60 \leq A \leq T</var>

<var>0 \leq H_i \leq 10^5</var>

All values in input are integers.

The solution is unique.

Input

Input is given from Standard Input in the following format:

<var>N</var>
<var>T</var> <var>A</var>
<var>H_1</var> <var>H_2</var> <var>...</var> <var>H_N</var>

Output

Print the index of the place where the palace should be built.

Sample Input 1

2
12 5
1000 2000

Sample Output 1

1

The average temperature of Place <var>1</var> is <var>12-1000 \times 0.006=6</var> degrees Celsius.

The average temperature of Place <var>2</var> is <var>12-2000 \times 0.006=0</var> degrees Celsius.

Thus, the palace should be built at Place <var>1</var>.

Sample Input 2

3
21 -11
81234 94124 52141

Sample Output 2

3

int solution() {
  int n = 0;
  int best_n = 0;
  int T = 0;
  int A = 0;
  int height_i[1001] = {0};
  int temp_average = 0;
  int temp_diff_from_a = 0;
  int new_temp_diff_from_a = 0;
  scanf("%d", &n);
  scanf("%d %d", &T, &A);
  for (int i = 0; i < n; i++) {
    scanf("%d", &height_i[i]);
  }

  for (int i = 0; i < n; i++) {
    temp_average = 1000 * T - height_i[i] * 6;
    new_temp_diff_from_a = abs((1000 * A) - temp_average);

    if (i == 0 || new_temp_diff_from_a < temp_diff_from_a) {
      temp_diff_from_a = new_temp_diff_from_a;
      best_n = i;
    }
  }
  printf("%d", best_n + 1);
  return 0;
}

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

    let mut ta = String::new();
    stdin().read_line(&mut ta).unwrap();
    let ta: Vec<isize> = ta.split_whitespace().flat_map(str::parse).collect();
    let (t, a) = (ta[0] * 1000, ta[1] * 1000);

    let mut harr = String::new();
    stdin().read_line(&mut harr).unwrap();
    let harr: Vec<_> = harr
        .split_whitespace()
        .flat_map(str::parse::<isize>)
        .map(|h| {
            let t = t - a - h * 6;
            if t >= 0 {
                t
            } else {
                -t
            }
        })
        .collect();
    let mut result = None;
    for (i, &h) in harr.iter().enumerate() {
        if result.is_none() {
            result = Some((i, h));
        } else if let Some((_, oh)) = result
            && oh > h {
                result = Some((i, h));
            }
    }
    if let Some((i, _)) = result {
        println!("{}", i + 1);
    }
}


Score : <var>100</var> points

Problem Statement

You are given a three-digit positive integer <var>N</var>.

Determine whether <var>N</var> is a palindromic number.

Here, a palindromic number is an integer that reads the same backward as forward in decimal notation.

Constraints

<var>100≤N≤999</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 a palindromic number, print Yes; otherwise, print No.

Sample Input 1

575

Sample Output 1

Yes

<var>N=575</var> is also <var>575</var> when read backward, so it is a palindromic number. You should print Yes.

Sample Input 2

123

Sample Output 2

No

<var>N=123</var> becomes <var>321</var> when read backward, so it is not a palindromic number. You should print No.

Sample Input 3

812

Sample Output 3

No

int solution(void) {
  int n = 0;

  scanf("%d", &n);

  if (100 <= n && n <= 999) {
    int up = 0;
    int dn = 0;
    up = n / 100;
    dn = n % 10;
    if (up == dn) {
      printf("Yes");
    } else {
      printf("No");
    }
  }

  return 0;
}

fn solution() {
    let mut s: String = String::new();
    std::io::stdin().read_to_string(&mut s).ok();
    let mut itr = s.split_whitespace();
    let s: Vec<char> = itr.next().unwrap().chars().collect();
    if s[0] == s[2] {
        println!("Yes");
    } else {
        println!("No");
    }
}


Score : <var>100</var> points

Problem Statement

You are given a string <var>S</var> of length <var>N</var> consisting of A, B and C, and an integer <var>K</var> which is between <var>1</var> and <var>N</var> (inclusive).
Print the string <var>S</var> after lowercasing the <var>K</var>-th character in it.

Constraints

<var>1 ≤ N ≤ 50</var>

<var>1 ≤ K ≤ N</var>

<var>S</var> is a string of length <var>N</var> consisting of A, B and C.

Input

Input is given from Standard Input in the following format:

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

Output

Print the string <var>S</var> after lowercasing the <var>K</var>-th character in it.

Sample Input 1

3 1
ABC

Sample Output 1

aBC

Sample Input 2

4 3
CABA

Sample Output 2

CAbA

int solution(void) {
  int N = 0;
  int K = 0;
  scanf("%d", &N);
  scanf("%d", &K);

  char line[N];

  for (int i = 0; i <= N; i++) {
    scanf("%c", &line[i]);
  }

  line[K] = line[K] + 32;

  for (int i = 0; i <= N; i++) {
    printf("%c", line[i]);
  }
  printf("\n");
  return 0;
}

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

    stdin().read_line(&mut nk).unwrap();
    stdin().read_line(&mut s).unwrap();

    let k: usize = nk.split_whitespace().collect::<Vec<_>>()[1]
        .parse::<usize>()
        .unwrap();

    let mut s = s.trim().chars().collect::<Vec<char>>();
    s[k - 1] = (s[k - 1] as u8 + 32) as char;

    println!("{}", s.into_iter().collect::<String>());
}


Score : <var>600</var> points

Problem Statement

We have a tree with <var>N</var> vertices. The <var>i</var>-th edge connects Vertex <var>A_i</var> and <var>B_i</var> bidirectionally.

Takahashi is standing at Vertex <var>u</var>, and Aoki is standing at Vertex <var>v</var>.

Now, they will play a game of tag as follows:

<var>1</var>. If Takahashi and Aoki are standing at the same vertex, the game ends. Otherwise, Takahashi moves to a vertex of his choice that is adjacent to his current vertex.

<var>2</var>. If Takahashi and Aoki are standing at the same vertex, the game ends. Otherwise, Aoki moves to a vertex of his choice that is adjacent to his current vertex.

<var>3</var>. Go back to step <var>1</var>.

Takahashi performs his moves so that the game ends as late as possible, while Aoki performs his moves so that the game ends as early as possible.

Find the number of moves Aoki will perform before the end of the game if both Takahashi and Aoki know each other's position and strategy.

It can be proved that the game is bound to end.

Constraints

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

<var>1 \leq u,v \leq N</var>

<var>u \neq v</var>

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

The given graph is a tree.

Input

Input is given from Standard Input in the following format:

<var>N</var> <var>u</var> <var>v</var>
<var>A_1</var> <var>B_1</var>
<var>:</var>
<var>A_{N-1}</var> <var>B_{N-1}</var>

Output

Print the number of moves Aoki will perform before the end of the game.

Sample Input 1

5 4 1
1 2
2 3
3 4
3 5

Sample Output 1

2

If both players play optimally, the game will progress as follows:

Takahashi moves to Vertex <var>3</var>.

Aoki moves to Vertex <var>2</var>.

Takahashi moves to Vertex <var>5</var>.

Aoki moves to Vertex <var>3</var>.

Takahashi moves to Vertex <var>3</var>.

Here, Aoki performs two moves.

Note that, in each move, it is prohibited to stay at the current vertex.

Sample Input 2

5 4 5
1 2
1 3
1 4
1 5

Sample Output 2

1

Sample Input 3

2 1 2
1 2

Sample Output 3

0

Sample Input 4

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

Sample Output 4

5

int solution() {
  int i;
  int u;
  int w;
  int N;
  int T;
  int A;
  scanf("%d %d %d", &N, &T, &A);
  list **adj = (list **)malloc(sizeof(list *) * (N + 1));
  list *d = (list *)malloc(sizeof(list) * (N - 1) * 2);
  for (i = 1; i <= N; i++) {
    adj[i] = NULL;
  }
  for (i = 0; i < N - 1; i++) {
    scanf("%d %d", &u, &w);
    d[i * 2].v = w;
    d[(i * 2) + 1].v = u;
    d[i * 2].next = adj[u];
    d[(i * 2) + 1].next = adj[w];
    adj[u] = &(d[i * 2]);
    adj[w] = &(d[(i * 2) + 1]);
  }

  int dist[2][100001] = {};
  int q[100001];
  int head;
  int tail;
  list *p;
  dist[0][A] = 1;
  q[0] = A;
  for (head = 0, tail = 1; head < tail; head++) {
    u = q[head];
    for (p = adj[u]; p != NULL; p = p->next) {
      w = p->v;
      if (dist[0][w] == 0) {
        dist[0][w] = dist[0][u] + 1;
        q[tail++] = w;
      }
    }
  }
  dist[1][T] = 1;
  q[0] = T;
  for (head = 0, tail = 1; head < tail; head++) {
    u = q[head];
    for (p = adj[u]; p != NULL; p = p->next) {
      w = p->v;
      if (dist[1][w] == 0 && dist[1][u] + 1 < dist[0][w]) {
        dist[1][w] = dist[1][u] + 1;
        q[tail++] = w;
      }
    }
  }

  int max = 0;
  for (i = 1; i <= N; i++) {
    if (dist[1][i] > 0 && dist[0][i] - 2 > max) {
      max = dist[0][i] - 2;
    }
  }
  printf("%d\n", max);
  fflush(stdout);
  return 0;
}

fn solution() {
    input! {
        n: usize,
        u: usize,
        v: usize,
        ab: [(usize, usize); n - 1]
    }
    let mut adjacency = vec![vec![]; n + 1];
    for (ai, bi) in ab {
        adjacency[ai].push(bi);
        adjacency[bi].push(ai);
    }

    let mut height = vec![0; n + 1];
    let mut parent = vec![0; n + 1];
    let mut queue = VecDeque::new();
    let mut visited = HashSet::new();
    queue.push_back(v);
    while !queue.is_empty() {
        let w = queue.pop_front().unwrap();
        visited.insert(w);
        for &x in adjacency[w].iter() {
            if !visited.contains(&x) {
                height[x] = height[w] + 1;
                parent[x] = w;
                queue.push_back(x);
            }
        }
    }

    let h = (height[u] - 1) / 2;
    let mut w = u;
    for _ in 0..h {
        w = parent[w];
    }

    let mut max_node = u;
    let mut queue = VecDeque::new();
    let mut visited = HashSet::new();
    queue.push_back(w);
    while !queue.is_empty() {
        let x = queue.pop_front().unwrap();
        visited.insert(x);
        for &y in adjacency[x].iter() {
            if !visited.contains(&y) && height[y] > height[w] {
                let _d = height[y] - height[w];
                if height[y] > height[max_node] {
                    max_node = y;
                }
                queue.push_back(y);
            }
        }
    }

    println!("{}", height[max_node] - 1);
}


Score : <var>200</var> points

Problem Statement

You have decided to write a book introducing good restaurants.
There are <var>N</var> restaurants that you want to introduce: Restaurant <var>1</var>, Restaurant <var>2</var>, <var>...</var>, Restaurant <var>N</var>. Restaurant <var>i</var> is in city <var>S_i</var>, and your assessment score of that restaurant on a <var>100</var>-point scale is <var>P_i</var>.
No two restaurants have the same score.

You want to introduce the restaurants in the following order:

The restaurants are arranged in lexicographical order of the names of their cities.

If there are multiple restaurants in the same city, they are arranged in descending order of score.

Print the identification numbers of the restaurants in the order they are introduced in the book.

Constraints

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

<var>S</var> is a string of length between <var>1</var> and <var>10</var> (inclusive) consisting of lowercase English letters.

<var>0 ≤ P_i ≤ 100</var>

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

<var>P_i ≠ P_j</var> <var>(1 ≤ i < j ≤ N)</var>

Input

Input is given from Standard Input in the following format:

<var>N</var>
<var>S_1</var> <var>P_1</var>
<var>:</var>
<var>S_N</var> <var>P_N</var>

Output

Print <var>N</var> lines. The <var>i</var>-th line (<var>1 ≤ i ≤ N</var>) should contain the identification number of the restaurant that is introduced <var>i</var>-th in the book.

Sample Input 1

6
khabarovsk 20
moscow 10
kazan 50
kazan 35
moscow 60
khabarovsk 40

Sample Output 1

3
4
6
1
5
2

The lexicographical order of the names of the three cities is kazan <var><</var> khabarovsk <var><</var> moscow. For each of these cities, the restaurants in it are introduced in descending order of score. Thus, the restaurants are introduced in the order <var>3,4,6,1,5,2</var>.

Sample Input 2

10
yakutsk 10
yakutsk 20
yakutsk 30
yakutsk 40
yakutsk 50
yakutsk 60
yakutsk 70
yakutsk 80
yakutsk 90
yakutsk 100

Sample Output 2

10
9
8
7
6
5
4
3
2
1

int solution(void) {
  int N = 0;
  int P[1000];
  char S[1000][100];
  int answer[1000] = {0};
  int count = 0;
  scanf("%d", &N);
  for (int i = 0; i < N; i++) {
    scanf("%s %d", S[i], &P[i]);
  }
  for (int i = 0; i < N; i++) {
    for (int j = 0; j < N; j++) {
      if (strcmp(S[i], S[j]) > 0) {
        count++;
      } else if (strcmp(S[i], S[j]) == 0) {
        if (P[i] < P[j]) {
          count++;
        }
      }
    }
    answer[count] = i;
    count = 0;
  }
  for (int i = 0; i < N; i++) {
    printf("%d\n", answer[i] + 1);
  }
  return 0;
}

fn solution() {
    let stdin = io::stdin();
    let mut stdin = stdin.lock();

    let n = {
        let mut input = String::new();
        stdin.read_line(&mut input).unwrap();
        let mut input = input.split_whitespace();
        input.next().unwrap().parse::<usize>().unwrap()
    };

    let mut vec = {
        let mut vec: Vec<(String, i32, usize)> = Vec::new();
        for i in 0..n {
            let mut input = String::new();
            stdin.read_line(&mut input).unwrap();
            let mut input = input.split_whitespace();

            vec.push((
                input.next().unwrap().to_string(),
                input.next().unwrap().parse::<i32>().unwrap(),
                i + 1,
            ));
        }
        vec
    };

    vec.sort_by(|a, b| {
        if a.0 == b.0 {
            b.1.cmp(&a.1)
        } else {
            a.0.cmp(&b.0)
        }
    });

    for x in vec.iter() {
        println!("{}", x.2);
    }
}


You are given two strings of equal length $$$s$$$ and $$$t$$$ consisting of lowercase Latin letters. You may perform any number (possibly, zero) operations on these strings.During each operation you choose two adjacent characters in any string and assign the value of the first character to the value of the second or vice versa.For example, if $$$s$$$ is "acbc" you can get the following strings in one operation:   "aabc" (if you perform $$$s_2 = s_1$$$);  "ccbc" (if you perform $$$s_1 = s_2$$$);  "accc" (if you perform $$$s_3 = s_2$$$ or $$$s_3 = s_4$$$);  "abbc" (if you perform $$$s_2 = s_3$$$);  "acbb" (if you perform $$$s_4 = s_3$$$); Note that you can also apply this operation to the string $$$t$$$.Please determine whether it is possible to transform $$$s$$$ into $$$t$$$, applying the operation above any number of times.Note that you have to answer $$$q$$$ independent queries.

int solution() {
  int m;
  scanf("%d", &m);
  while (m--) {
    char s[101];
    char t[101];
    scanf("%s %s", s, t);
    int n = strlen(s);
    int flag = 0;
    int cnts[26] = {0};
    int cntf[26] = {0};
    for (int i = 0; i < n; i++) {
      cnts[s[i] - 'a']++;
      cntf[t[i] - 'a']++;
    }
    for (int i = 0; i < 26; i++) {
      if (cnts[i] != 0 && cntf[i] != 0) {
        printf("YES\n");
        flag = 1;
        break;
      }
    }
    if (!flag) {
      printf("NO\n");
    }
  }
  return 0;
}

fn solution() {
    let mut buffer = String::new();
    io::stdin()
        .read_line(&mut buffer)
        .expect("failed to read input");
    let q: u8 = buffer.trim().parse().expect("invalid input");

    for _ in 0..q {
        let mut input = String::new();
        io::stdin()
            .read_line(&mut input)
            .expect("failed to read input");
        let s = input.trim();

        let mut input = String::new();
        io::stdin()
            .read_line(&mut input)
            .expect("failed to read input");
        let t = input.trim();

        let mut s_chars = [false; 26];
        for c in s.chars() {
            s_chars[c as usize - 'a' as usize] = true;
        }

        let mut found = false;
        for c in t.chars() {
            if s_chars[c as usize - 'a' as usize] {
                found = true;
                break;
            }
        }

        println!("{}", if found { "YES" } else { "NO" });
    }
}


Official House

You manage 4 buildings, each of which has 3 floors, each of which consists of 10 rooms. Write a program which reads a sequence of tenant/leaver notices, and reports the number of tenants for each room.

  For each notice, you are given four integers <var>b</var>, <var>f</var>, <var>r</var> and <var>v</var> which represent that <var>v</var> persons entered to room <var>r</var> of <var>f</var>th floor at building <var>b</var>. If <var>v</var> is negative, it means that <var>−v</var> persons left.

Assume that initially no person lives in the building.

Input

  In the first line, the number of notices <var>n</var> is given. In the following <var>n</var> lines, a set of four integers <var>b</var>, <var>f</var>, <var>r</var> and <var>v</var> which represents <var>i</var>th notice is given in a line.

Output

For each building, print the information of 1st, 2nd and 3rd floor in this order. For each floor information, print the number of tenants of 1st, 2nd, .. and 10th room in this order. Print a single space character before the number of tenants. Print "####################" (20 '#') between buildings.

Constraints

No incorrect building, floor and room numbers are given.

0 ≤ the number of tenants during the management ≤ 9

Sample Input

3
1 1 3 8
3 2 2 7
4 3 8 1

Sample Output

 0 0 8 0 0 0 0 0 0 0
 0 0 0 0 0 0 0 0 0 0
 0 0 0 0 0 0 0 0 0 0
####################
 0 0 0 0 0 0 0 0 0 0
 0 0 0 0 0 0 0 0 0 0
 0 0 0 0 0 0 0 0 0 0
####################
 0 0 0 0 0 0 0 0 0 0
 0 7 0 0 0 0 0 0 0 0
 0 0 0 0 0 0 0 0 0 0
####################
 0 0 0 0 0 0 0 0 0 0
 0 0 0 0 0 0 0 0 0 0
 0 0 0 0 0 0 0 1 0 0

fn solution() {
    let mut line = String::new();
    io::stdin().read_line(&mut line).ok();
    let n = line.trim().parse::<i32>().unwrap();

    let mut notices = Vec::new();
    for _ in 0..n {
        let mut line = String::new();
        io::stdin().read_line(&mut line).ok();
        let mut iter = line.split_whitespace();
        let b = iter.next().unwrap().parse::<usize>().unwrap();
        let f = iter.next().unwrap().parse::<usize>().unwrap();
        let r = iter.next().unwrap().parse::<usize>().unwrap();
        let v = iter.next().unwrap().parse::<i32>().unwrap();
        notices.push((b, f, r, v));
    }

    let n_buildings = 4;
    let n_floors = n_buildings * 3;
    let n_rooms = n_floors * 10;
    let mut rooms = vec![0; n_rooms];
    for notice in notices {
        let (b, f, r, v) = notice;
        rooms[(b - 1) * 30 + (f - 1) * 10 + (r - 1)] += v;
    }

    for b in 0..4 {
        if b != 0 {
            println!("####################");
        }
        for f in 0..3 {
            for r in 0..10 {
                print!(" {}", rooms[b * 30 + f * 10 + r]);
            }
            println!();
        }
    }
}


Integral

Write a program which computes the area of a shape represented by the following three lines:

$y = x^2$

$y = 0$

$x = 600$

It is clear that the area is $72000000$, if you use an integral you learn in high school. On the other hand, we can obtain an approximative area of the shape by adding up areas of many rectangles in the shape as shown in the following figure:

$f(x) = x^2$

The approximative area $s$ where the width of the rectangles is $d$ is:

area of rectangle where its width is $d$ and height is $f(d)$ $+$

area of rectangle where its width is $d$ and height is $f(2d)$ $+$

area of rectangle where its width is $d$ and height is $f(3d)$ $+$

...

area of rectangle where its width is $d$ and height is $f(600 - d)$

The more we decrease $d$, the higer-precision value which is close to $72000000$ we could obtain. Your program should read the integer $d$ which is a divisor of $600$, and print the area $s$.

Input

The input consists of several datasets. Each dataset consists of an integer $d$ in a line. The number of datasets is less than or equal to 20.

Output

For each dataset, print the area $s$ in a line.

Sample Input

20
10

Output for the Sample Input

68440000
70210000

fn solution() {
    let input = BufReader::new(stdin());
    for line in input.lines() {
        let d = line.unwrap().parse::<u32>().unwrap();
        let mut s = 0;
        let mut x = d;
        while x < 600 {
            s += x * x * d;
            x += d;
        }
        println!("{}", s);
    }
}


Score : <var>400</var> points

Problem Statement

Given is a rooted tree with <var>N</var> vertices numbered <var>1</var> to <var>N</var>.
The root is Vertex <var>1</var>, and the <var>i</var>-th edge <var>(1 \leq i \leq N - 1)</var> connects Vertex <var>a_i</var> and <var>b_i</var>.

Each of the vertices has a counter installed. Initially, the counters on all the vertices have the value <var>0</var>.

Now, the following <var>Q</var> operations will be performed:

Operation <var>j</var> <var>(1 \leq j \leq Q)</var>: Increment by <var>x_j</var> the counter on every vertex contained in the subtree rooted at Vertex <var>p_j</var>.

Find the value of the counter on each vertex after all operations.

Constraints

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

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

<var>1 \leq a_i < b_i \leq N</var>

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

<var>1 \leq x_j \leq 10^4</var>

The given graph is a tree.

All values in input are integers.

Input

Input is given from Standard Input in the following format:

<var>N</var> <var>Q</var>
<var>a_1</var> <var>b_1</var>
<var>:</var>
<var>a_{N-1}</var> <var>b_{N-1}</var>
<var>p_1</var> <var>x_1</var>
<var>:</var>
<var>p_Q</var> <var>x_Q</var>

Output

Print the values of the counters on Vertex <var>1, 2, \ldots, N</var> after all operations, in this order, with spaces in between.

Sample Input 1

4 3
1 2
2 3
2 4
2 10
1 100
3 1

Sample Output 1

100 110 111 110

The tree in this input is as follows:

Each operation changes the values of the counters on the vertices as follows:

Operation <var>1</var>: Increment by <var>10</var> the counter on every vertex contained in the subtree rooted at Vertex <var>2</var>, that is, Vertex <var>2, 3, 4</var>. The values of the counters on Vertex <var>1, 2, 3, 4</var> are now <var>0, 10, 10, 10</var>, respectively.

Operation <var>2</var>: Increment by <var>100</var> the counter on every vertex contained in the subtree rooted at Vertex <var>1</var>, that is, Vertex <var>1, 2, 3, 4</var>. The values of the counters on Vertex <var>1, 2, 3, 4</var> are now <var>100, 110, 110, 110</var>, respectively.

Operation <var>3</var>: Increment by <var>1</var> the counter on every vertex contained in the subtree rooted at Vertex <var>3</var>, that is, Vertex <var>3</var>. The values of the counters on Vertex <var>1, 2, 3, 4</var> are now <var>100, 110, 111, 110</var>, respectively.

Sample Input 2

6 2
1 2
1 3
2 4
3 6
2 5
1 10
1 10

Sample Output 2

20 20 20 20 20 20

int solution(void) {
  int n;
  int q;
  int i;
  scanf("%d%d", &n, &q);
  Node *node;
  node = (Node *)calloc(n + 1, sizeof(Node));
  int a;
  int b;
  for (i = 1; i < n; i++) {
    scanf("%d%d", &a, &b);
    (node[b].parent) = &node[a];
  }
  int p;
  int x;
  for (i = 0; i < q; i++) {
    scanf("%d%d", &p, &x);
    (node[p].cnt) += x;
  }

  for (i = 1; i <= n; i++) {
    if (i != 1) {
      node[i].cnt += (node[i].parent)->cnt;
    }
    printf("%lld ", node[i].cnt);
  }
  printf("\n");
  free(node);
  return 0;
}

fn solution() {
    let mut buf = String::new();
    std::io::stdin().read_to_string(&mut buf).ok();
    let mut it = buf.split_whitespace();
    let n = it.next().unwrap().parse::<usize>().unwrap();
    let q = it.next().unwrap().parse::<usize>().unwrap();
    let mut g = vec![Vec::new(); n];
    for _ in 0..n - 1 {
        let a = it.next().unwrap().parse::<usize>().unwrap() - 1;
        let b = it.next().unwrap().parse::<usize>().unwrap() - 1;
        g[a].push(b);
        g[b].push(a);
    }
    let mut res = vec![0; n];
    for _ in 0..q {
        let p = it.next().unwrap().parse::<usize>().unwrap() - 1;
        let x = it.next().unwrap().parse::<i64>().unwrap();
        res[p] += x;
    }
    let mut qu = VecDeque::new();
    qu.push_back(0);
    let mut visit = vec![0; n];
    visit[0] = 1;
    while !qu.is_empty() {
        let p = qu.pop_front().unwrap();
        for &t in &g[p] {
            if visit[t] == 1 {
                continue;
            }
            visit[t] = 1;
            res[t] += res[p];
            qu.push_back(t);
        }
    }
    print!("{}", res[0]);
    for i in 1..n {
        print!(" {}", res[i]);
    }
    println!();
}


Score: <var>300</var> points

Problem Statement

We have a <var>3 \times 3</var> grid. A number <var>c_{i, j}</var> is written in the square <var>(i, j)</var>, where <var>(i, j)</var> denotes the square at the <var>i</var>-th row from the top and the <var>j</var>-th column from the left.

According to Takahashi, there are six integers <var>a_1, a_2, a_3, b_1, b_2, b_3</var> whose values are fixed, and the number written in the square <var>(i, j)</var> is equal to <var>a_i + b_j</var>.

Determine if he is correct.

Constraints

<var>c_{i, j} \ (1 \leq i \leq 3, 1 \leq j \leq 3)</var> is an integer between <var>0</var> and <var>100</var> (inclusive).

Input

Input is given from Standard Input in the following format:

<var>c_{1,1}</var> <var>c_{1,2}</var> <var>c_{1,3}</var>
<var>c_{2,1}</var> <var>c_{2,2}</var> <var>c_{2,3}</var>
<var>c_{3,1}</var> <var>c_{3,2}</var> <var>c_{3,3}</var>

Output

If Takahashi's statement is correct, print Yes; otherwise, print No.

Sample Input 1

1 0 1
2 1 2
1 0 1

Sample Output 1

Yes

Takahashi is correct, since there are possible sets of integers such as: <var>a_1=0,a_2=1,a_3=0,b_1=1,b_2=0,b_3=1</var>.

Sample Input 2

2 2 2
2 1 2
2 2 2

Sample Output 2

No

Takahashi is incorrect in this case.

Sample Input 3

0 8 8
0 8 8
0 8 8

Sample Output 3

Yes

Sample Input 4

1 8 6
2 9 7
0 7 7

Sample Output 4

No

fn solution() {
    let mut s: String = String::new();
    std::io::stdin().read_to_string(&mut s).ok();
    let mut itr = s.split_whitespace();
    let c: Vec<i32> = (0..9)
        .map(|_| itr.next().unwrap().parse().unwrap())
        .collect();

    for a1 in 1..4 {
        let b1 = c[0] - a1;
        let b2 = c[1] - a1;
        let b3 = c[2] - a1;
        let a2 = c[3] - b1;
        let a3 = c[6] - b1;
        if c[4] == a2 + b2 && c[5] == a2 + b3 && c[7] == a3 + b2 && c[8] == a3 + b3 {
            println!("Yes");
            return;
        }
    }
    println!("No");
}


Score : <var>100</var> points

Problem Statement

Snuke has a favorite restaurant.

The price of any meal served at the restaurant is <var>800</var> yen (the currency of Japan), and each time a customer orders <var>15</var> meals, the restaurant pays <var>200</var> yen back to the customer.

So far, Snuke has ordered <var>N</var> meals at the restaurant.
Let the amount of money Snuke has paid to the restaurant be <var>x</var> yen, and let the amount of money the restaurant has paid back to Snuke be <var>y</var> yen.
Find <var>x-y</var>.

Constraints

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

Input

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

<var>N</var>

Output

Print the answer.

Sample Input 1

20

Sample Output 1

15800

So far, Snuke has paid <var>16000</var> yen, and the restaurant has paid back <var>200</var> yen. Thus, the answer is <var>15800</var>.

Sample Input 2

60

Sample Output 2

47200

Snuke has paid <var>48000</var> yen for <var>60</var> meals, and the restaurant has paid back <var>800</var> yen.

int solution() {
  int *var = malloc(sizeof(int));
  scanf("%d", var);
  printf("%d\n", (*var * 800) - (200 * (*var / 15)));
  free(var);
  return 0;
}

fn solution() {
    use std::io;

    let mut input = String::new();
    let _ = io::stdin().read_line(&mut input);
    let num = input.trim().parse::<usize>().unwrap();

    println!("{}", num * 800 - (num / 15) * 200);
}


Score : <var>200</var> points

Problem Statement

An <var>X</var>-layered kagami mochi <var>(X ≥ 1)</var> is a pile of <var>X</var> round mochi (rice cake) stacked vertically where each mochi (except the bottom one) has a smaller diameter than that of the mochi directly below it. For example, if you stack three mochi with diameters of <var>10</var>, <var>8</var> and <var>6</var> centimeters from bottom to top in this order, you have a <var>3</var>-layered kagami mochi; if you put just one mochi, you have a <var>1</var>-layered kagami mochi.

Lunlun the dachshund has <var>N</var> round mochi, and the diameter of the <var>i</var>-th mochi is <var>d_i</var> centimeters. When we make a kagami mochi using some or all of them, at most how many layers can our kagami mochi have?

Constraints

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

<var>1 ≤ d_i ≤ 100</var>

All input values are integers.

Input

Input is given from Standard Input in the following format:

<var>N</var>
<var>d_1</var>
<var>:</var>
<var>d_N</var>

Output

Print the maximum number of layers in a kagami mochi that can be made.

Sample Input 1

4
10
8
8
6

Sample Output 1

3

If we stack the mochi with diameters of <var>10</var>, <var>8</var> and <var>6</var> centimeters from bottom to top in this order, we have a <var>3</var>-layered kagami mochi, which is the maximum number of layers.

Sample Input 2

3
15
15
15

Sample Output 2

1

When all the mochi have the same diameter, we can only have a <var>1</var>-layered kagami mochi.

Sample Input 3

7
50
30
50
100
50
80
30

Sample Output 3

4

int solution() {
  int num = 0;
  int num_mochies = 0;

  int num_steps = 1;

  scanf("%d\n", &num_mochies);

  int mochies[num_mochies];

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

  for (int j = 0; j < num_mochies; j++) {
    for (int i = 0; i < num_mochies - (j + 1); i++) {
      if (mochies[i] < mochies[i + 1]) {
        num = mochies[i];
        mochies[i] = mochies[i + 1];
        mochies[i + 1] = num;
      }
    }
  }

  for (int k = 0; k < num_mochies - 1; k++) {
    if (mochies[k] > mochies[k + 1]) {
      num_steps++;
    }
  }

  printf("%d\n", num_steps);

  return 0;
}

fn solution() {
    let mut buf: String = String::new();
    io::stdin().read_line(&mut buf).unwrap();
    let n: i32 = buf.trim().parse::<i32>().unwrap();
    let mut mochi: Vec<i32> = Vec::new();
    for _ in 0..n {
        let mut buf: String = String::new();
        io::stdin().read_line(&mut buf).unwrap();
        let m: i32 = buf.trim().parse::<i32>().unwrap();
        mochi.push(m);
    }
    mochi.sort();
    mochi.dedup();
    println!("{}", mochi.len());
}


Simultaneous Equation

Write a program which solve a simultaneous equation:

<var> ax + by = c</var>

<var> dx + ey = f</var>

The program should print <var>x</var> and <var>y</var> for given <var>a</var>, <var>b</var>, <var>c</var>, <var>d</var>, <var>e</var> and <var>f</var> (-1,000 ≤ <var>a, b, c, d, e, f</var> ≤ 1,000). You can suppose that given equation has a unique solution.

Input

The input consists of several data sets, 1 line for each data set. In a data set, there will be <var>a, b, c, d, e, f</var> separated by a single space. The input terminates with EOF.

Output

For each data set, print <var>x</var> and <var>y</var> separated by a single space. Print the solution to three places of decimals. Round off the solution to three decimal places.

Sample Input 1

1 2 3 4 5 6
2 -1 -2 -1 -1 -5

Output for the Sample Input 1

-1.000 2.000
1.000 4.000

Sample Input 2

2 -1 -3 1 -1 -3
2 -1 -3 -9 9 27

Output for the Sample Input 2

0.000 3.000
0.000 3.000

int solution(void) {
  float a = 0;
  float b = 0;
  float c = 0;
  float d = 0;
  float e = 0;
  float f = 0;
  while (scanf("%f %f %f %f %f %f", &a, &b, &c, &d, &e, &f) != EOF) {
    float x = 0;
    float y = 0;
    y = (c * d - a * f) / (b * d - a * e);
    x = c / a - (b / a) * y;
    x = roundf(1000 * x) / 1000;
    y = roundf(1000 * y) / 1000;
    printf("%.3f %.3f\n", x, y);
  }
  return 0;
}

fn solution() {
    let input = BufReader::new(stdin());
    for line in input.lines() {
        let v: Vec<f32> = line
            .unwrap()
            .split_whitespace()
            .filter_map(|x| x.parse::<f32>().ok())
            .collect();
        let x = (v[2] * v[4] - v[1] * v[5]) / (v[0] * v[4] - v[1] * v[3]);
        let y = (v[2] * v[3] - v[0] * v[5]) / (v[1] * v[3] - v[0] * v[4]);
        println!("{:.3} {:.3}", x, y);
    }
}


After their adventure with the magic mirror Kay and Gerda have returned home and sometimes give free ice cream to kids in the summer.At the start of the day they have x ice cream packs. Since the ice cream is free, people start standing in the queue before Kay and Gerda's house even in the night. Each person in the queue wants either to take several ice cream packs for himself and his friends or to give several ice cream packs to Kay and Gerda (carriers that bring ice cream have to stand in the same queue).If a carrier with d ice cream packs comes to the house, then Kay and Gerda take all his packs. If a child who wants to take d ice cream packs comes to the house, then Kay and Gerda will give him d packs if they have enough ice cream, otherwise the child will get no ice cream at all and will leave in distress.Kay wants to find the amount of ice cream they will have after all people will leave from the queue, and Gerda wants to find the number of distressed kids.

int solution() {
  int n = 0;
  int i = 0;
  int s = 1;
  int distress = 0;
  long long iceCream = 0;
  long long in = 0;
  char sig;
  scanf("%d %lld\n", &n, &iceCream);
  for (i = 0; i < n; i++) {
    scanf("%c %lld\n", &sig, &in);
    if (sig == '-') {
      s = -1;
    } else {
      s = 1;
    }
    if (iceCream + (in * s) < 0) {
      distress++;
    } else {
      iceCream += (in * s);
    }
  }
  printf("%lld %d\n", iceCream, distress);
  return 0;
}

fn solution() {
    use std::io;
    let mut buf = String::new();
    io::stdin().read_line(&mut buf).unwrap();
    let n;
    let mut x;
    {
        let mut ns = buf.split(" ").map(|str| str.trim().parse::<u64>().unwrap());
        n = ns.next().unwrap();
        x = ns.next().unwrap();
    }
    let mut distressed = 0;
    for _ in 0..n {
        buf.clear();
        io::stdin().read_line(&mut buf).unwrap();
        let mut b = buf.split(" ");
        let sign = b.next().unwrap().trim();
        let m = b.next().unwrap().trim().parse::<u64>().unwrap();
        if sign == "+" {
            x += m;
        } else if sign == "-" {
            if x >= m {
                x -= m;
            } else {
                distressed += 1;
            }
        }
    }
    println!("{} {}", x, distressed);
}


You have a sequence $$$a$$$ with $$$n$$$ elements $$$1, 2, 3, \dots, k - 1, k, k - 1, k - 2, \dots, k - (n - k)$$$ ($$$k \le n < 2k$$$).Let's call as inversion in $$$a$$$ a pair of indices $$$i < j$$$ such that $$$a[i] > a[j]$$$.Suppose, you have some permutation $$$p$$$ of size $$$k$$$ and you build a sequence $$$b$$$ of size $$$n$$$ in the following manner: $$$b[i] = p[a[i]]$$$.Your goal is to find such permutation $$$p$$$ that the total number of inversions in $$$b$$$ doesn't exceed the total number of inversions in $$$a$$$, and $$$b$$$ is lexicographically maximum.Small reminder: the sequence of $$$k$$$ integers is called a permutation if it contains all integers from $$$1$$$ to $$$k$$$ exactly once.Another small reminder: a sequence $$$s$$$ is lexicographically smaller than another sequence $$$t$$$, if either $$$s$$$ is a prefix of $$$t$$$, or for the first $$$i$$$ such that $$$s_i \ne t_i$$$, $$$s_i < t_i$$$ holds (in the first position that these sequences are different, $$$s$$$ has smaller number than $$$t$$$).

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

    let mut lines = input.lines();

    lines.next();

    for line in lines {
        let mut it = line.split_whitespace().map(|x| x.parse::<usize>().unwrap());
        let n = it.next().unwrap();
        let k = it.next().unwrap();

        let mut p: Vec<_> = (1..=k).collect();

        p[2 * k - n - 1..].reverse();

        for x in p {
            print!("{} ", x);
        }
        println!();
    }
}


You are both a shop keeper and a shop assistant at a small nearby shop. You have $$$n$$$ goods, the $$$i$$$-th good costs $$$a_i$$$ coins.You got tired of remembering the price of each product when customers ask for it, thus you decided to simplify your life. More precisely you decided to set the same price for all $$$n$$$ goods you have.However, you don't want to lose any money so you want to choose the price in such a way that the sum of new prices is not less than the sum of the initial prices. It means that if you sell all $$$n$$$ goods for the new price, you will receive at least the same (or greater) amount of money as if you sell them for their initial prices.On the other hand, you don't want to lose customers because of big prices so among all prices you can choose you need to choose the minimum one.So you need to find the minimum possible equal price of all $$$n$$$ goods so if you sell them for this price, you will receive at least the same (or greater) amount of money as if you sell them for their initial prices.You have to answer $$$q$$$ independent queries.

int solution() {
  int t;
  scanf("%d", &t);
  for (int i = 1; i <= t; i++) {
    int n;
    scanf("%d", &n);
    int a[n];
    int sum = 0;
    for (int j = 0; j < n; j++) {
      scanf("%d", &a[j]);
      sum += a[j];
    }
    int c = sum / n;
    if ((c * n) < sum) {
      c++;
    }
    printf("%d\n", c);
  }
  return 0;
}

fn solution() {
    let stdin = std::io::stdin();
    let mut buf = String::new();
    stdin.read_line(&mut buf).unwrap();
    let tests: isize = buf.trim().parse().unwrap();
    for _ in 0..tests {
        buf.clear();
        stdin.read_line(&mut buf).unwrap();
        let n: i64 = buf.trim().parse().unwrap();
        buf.clear();
        stdin.read_line(&mut buf).unwrap();
        let total = buf
            .split_whitespace()
            .map(|x| x.parse::<i64>().unwrap())
            .sum::<i64>();
        println!("{}", (total + n - 1) / n);
    }
}


Ujan decided to make a new wooden roof for the house. He has $$$n$$$ rectangular planks numbered from $$$1$$$ to $$$n$$$. The $$$i$$$-th plank has size $$$a_i \times 1$$$ (that is, the width is $$$1$$$ and the height is $$$a_i$$$).Now, Ujan wants to make a square roof. He will first choose some of the planks and place them side by side in some order. Then he will glue together all of these planks by their vertical sides. Finally, he will cut out a square from the resulting shape in such a way that the sides of the square are horizontal and vertical.For example, if Ujan had planks with lengths $$$4$$$, $$$3$$$, $$$1$$$, $$$4$$$ and $$$5$$$, he could choose planks with lengths $$$4$$$, $$$3$$$ and $$$5$$$. Then he can cut out a $$$3 \times 3$$$ square, which is the maximum possible. Note that this is not the only way he can obtain a $$$3 \times 3$$$ square.  What is the maximum side length of the square Ujan can get?

fn solution() {
    let mut input = String::new();
    let mut pieces: Vec<i32> = Vec::new();

    stdin().read_line(&mut input);

    let test_count = input.trim().parse::<i32>().unwrap();
    input.clear();

    for _test in 0..test_count {
        pieces.clear();
        stdin().read_line(&mut input);
        let _piece_count = input.trim().parse::<i32>().unwrap();
        input.clear();
        stdin().read_line(&mut input);
        for piece in input.split(" ") {
            pieces.push(piece.trim().parse::<i32>().unwrap());
        }
        input.clear();

        pieces.sort_by(|l, r| Ord::cmp(&r, &l));

        let mut saved_value = 1;

        for (i, &piece) in pieces.iter().enumerate() {
            if piece as usize >= (i + 1) {
                saved_value = i + 1;
            } else {
                break;
            }
        }

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


Score : <var>100</var> points

Problem Statement

In some other world, today is December <var>D</var>-th.

Write a program that prints Christmas if <var>D = 25</var>, Christmas Eve if <var>D = 24</var>, Christmas Eve Eve if <var>D = 23</var> and Christmas Eve Eve Eve if <var>D = 22</var>.

Constraints

<var>22 \leq D \leq 25</var>

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

Input

Input is given from Standard Input in the following format:

<var>D</var>

Output

Print the specified string (case-sensitive).

Sample Input 1

25

Sample Output 1

Christmas

Sample Input 2

22

Sample Output 2

Christmas Eve Eve Eve

Be sure to print spaces between the words.

int solution() {
  int D = 0;
  scanf("%d", &D);
  printf("Christmas ");
  while (D != 25) {
    printf("Eve ");
    D++;
  }
  printf("\n");
  return 0;
}

fn solution() {
    let mut day = String::new();
    io::stdin()
        .read_line(&mut day)
        .expect("Failed to read line");
    let num = (*day).trim().parse::<u32>().unwrap();
    match num {
        25 => println!("Christmas"),
        24 => println!("Christmas Eve"),
        23 => println!("Christmas Eve Eve"),
        22 => println!("Christmas Eve Eve Eve"),
        _ => println!("Invalid Input"),
    }
}


Score : <var>400</var> points

Problem Statement

Your friend gave you a dequeue <var>D</var> as a birthday present.

<var>D</var> is a horizontal cylinder that contains a row of <var>N</var> jewels.

The values of the jewels are <var>V_1, V_2, ..., V_N</var> from left to right. There may be jewels with negative values.

In the beginning, you have no jewel in your hands.

You can perform at most <var>K</var> operations on <var>D</var>, chosen from the following, at most <var>K</var> times (possibly zero):

Operation A: Take out the leftmost jewel contained in <var>D</var> and have it in your hand. You cannot do this operation when <var>D</var> is empty.

Operation B: Take out the rightmost jewel contained in <var>D</var> and have it in your hand. You cannot do this operation when <var>D</var> is empty.

Operation C: Choose a jewel in your hands and insert it to the left end of <var>D</var>. You cannot do this operation when you have no jewel in your hand.

Operation D: Choose a jewel in your hands and insert it to the right end of <var>D</var>. You cannot do this operation when you have no jewel in your hand.

Find the maximum possible sum of the values of jewels in your hands after the operations.

Constraints

All values in input are integers.

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

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

<var>-10^7 \leq V_i \leq 10^7</var>

Input

Input is given from Standard Input in the following format:

<var>N</var> <var>K</var>
<var>V_1</var> <var>V_2</var> <var>...</var> <var>V_N</var>

Output

Print the maximum possible sum of the values of jewels in your hands after the operations.

Sample Input 1

6 4
-10 8 2 1 2 6

Sample Output 1

14

After the following sequence of operations, you have two jewels of values <var>8</var> and <var>6</var> in your hands for a total of <var>14</var>, which is the maximum result.

Do operation A. You take out the jewel of value <var>-10</var> from the left end of <var>D</var>.

Do operation B. You take out the jewel of value <var>6</var> from the right end of <var>D</var>.

Do operation A. You take out the jewel of value <var>8</var> from the left end of <var>D</var>.

Do operation D. You insert the jewel of value <var>-10</var> to the right end of <var>D</var>.

Sample Input 2

6 4
-6 -100 50 -2 -5 -3

Sample Output 2

44

Sample Input 3

6 3
-6 -100 50 -2 -5 -3

Sample Output 3

0

It is optimal to do no operation.

int solution() {
  int K;
  int N;
  int V[51];
  int maxim;
  int sum1;
  int sum2;
  int ind;
  int list[51][51];
  int index[50];
  int index2[50] = {0};
  scanf("%d %d", &N, &K);
  for (int i = 0; i < N; i++) {
    scanf("%d", &V[i]);
  }
  sum1 = 0;
  for (int i = 0; i <= N; i++) {
    sum2 = 0;
    for (int j = 0; j <= N - i; j++) {
      list[i][j] = sum1 + sum2;
      if (j != N) {
        sum2 += V[N - j - 1];
      }
    }
    sum1 += V[i];
  }
  for (int i = 0; i < N; i++) {
    maxim = 10000001;
    ind = -1;
    for (int j = 0; j < N; j++) {
      if (index2[j] == 0 && maxim > V[j]) {
        ind = j;
        maxim = V[j];
      }
    }
    index[i] = ind;
    index2[ind] = 1;
  }
  maxim = 0;
  for (int i = 0; i <= N; i++) {
    for (int j = 0; j <= N - i; j++) {
      sum1 = 0;
      sum2 = list[i][j];
      for (int k = 0; k < N; k++) {
        if (sum1 >= K - i - j) {
          break;
        }
        if ((index[k] < i || index[k] >= N - j) && V[index[k]] < 0) {
          sum2 -= V[index[k]];
          sum1++;
        }
      }
      if (maxim < sum2 && i + j <= K) {
        maxim = sum2;
      }
    }
  }
  printf("%d\n", maxim);
  return 0;
}

fn solution() {
    input! {
        n: usize,
        k: usize,
        d: [i64; n],
    }
    let n_act = min(n, k);
    let mut ans = 0;
    for a in 0..n_act + 1 {
        for b in 0..n_act + 1 - a {
            let rm = k - a - b;
            let mut acc_vec: Vec<i64> = d
                .iter()
                .take(a)
                .chain(d.iter().skip(n - b))
                .cloned()
                .collect();
            acc_vec.sort();
            let acc = acc_vec
                .iter()
                .enumerate()
                .filter(|&(i, x)| rm <= i || (rm > i && *x > 0))
                .map(|(_, x)| *x)
                .sum();
            ans = max(ans, acc);
        }
    }
    println!("{}", ans);
}


Score : <var>100</var> points

Problem Statement

You will be given a string <var>S</var> of length <var>3</var> representing the weather forecast for three days in the past.

The <var>i</var>-th character <var>(1 \leq i \leq 3)</var> of <var>S</var> represents the forecast for the <var>i</var>-th day. S, C, and R stand for sunny, cloudy, and rainy, respectively.

You will also be given a string <var>T</var> of length <var>3</var> representing the actual weather on those three days.

The <var>i</var>-th character <var>(1 \leq i \leq 3)</var> of <var>S</var> represents the actual weather on the <var>i</var>-th day. S, C, and R stand for sunny, cloudy, and rainy, respectively.

Print the number of days for which the forecast was correct.

Constraints

<var>S</var> and <var>T</var> are strings of length <var>3</var> each.

<var>S</var> and <var>T</var> consist of S, C, and R.

Input

Input is given from Standard Input in the following format:

<var>S</var>
<var>T</var>

Output

Print the number of days for which the forecast was correct.

Sample Input 1

CSS
CSR

Sample Output 1

2

For the first day, it was forecast to be cloudy, and it was indeed cloudy.

For the second day, it was forecast to be sunny, and it was indeed sunny.

For the third day, it was forecast to be sunny, but it was rainy.

Thus, the forecast was correct for two days in this case.

Sample Input 2

SSR
SSR

Sample Output 2

3

Sample Input 3

RRR
SSS

Sample Output 3

0

fn solution() {
    let mut s = String::new();
    stdin().read_line(&mut s).unwrap();
    let s = s.trim();

    let mut t = String::new();
    stdin().read_line(&mut t).unwrap();
    let t = t.trim();

    println!(
        "{}",
        s.chars().zip(t.chars()).filter(|&(a, b)| a == b).count()
    );
}


Score : <var>300</var> points

Problem Statement

In a public bath, there is a shower which emits water for <var>T</var> seconds when the switch is pushed.

If the switch is pushed when the shower is already emitting water, from that moment it will be emitting water for <var>T</var> seconds.
Note that it does not mean that the shower emits water for <var>T</var> additional seconds.

<var>N</var> people will push the switch while passing by the shower.
The <var>i</var>-th person will push the switch <var>t_i</var> seconds after the first person pushes it.

How long will the shower emit water in total?

Constraints

<var>1 ≤ N ≤ 200,000</var>

<var>1 ≤ T ≤ 10^9</var>

<var>0 = t_1 < t_2 < t_3 < , ..., < t_{N-1} < t_N ≤ 10^9</var>

<var>T</var> and each <var>t_i</var> are integers.

Input

Input is given from Standard Input in the following format:

<var>N</var> <var>T</var>
<var>t_1</var> <var>t_2</var> ... <var>t_N</var>

Output

Assume that the shower will emit water for a total of <var>X</var> seconds. Print <var>X</var>.

Sample Input 1

2 4
0 3

Sample Output 1

7

Three seconds after the first person pushes the water, the switch is pushed again and the shower emits water for four more seconds, for a total of seven seconds.

Sample Input 2

2 4
0 5

Sample Output 2

8

One second after the shower stops emission of water triggered by the first person, the switch is pushed again.

Sample Input 3

4 1000000000
0 1000 1000000 1000000000

Sample Output 3

2000000000

Sample Input 4

1 1
0

Sample Output 4

1

Sample Input 5

9 10
0 3 5 7 100 110 200 300 311

Sample Output 5

67

int solution() {
  int n;
  int t;
  scanf("%d%d", &n, &t);
  int a[n];
  scanf("%d", &a[0]);
  long long ans = 0;
  for (int i = 1; i < n; i++) {
    scanf("%d", &a[i]);
    if (a[i] - a[i - 1] >= t) {
      ans += t;
    } else {
      ans += (a[i] - a[i - 1]);
    }
  }
  ans += t;
  printf("%lld", ans);
  return 0;
}

fn solution() {
    let mut s = String::new();
    io::stdin().read_line(&mut s).unwrap();
    let s = s.trim();

    let nt: Vec<&str> = s.split(" ").collect();
    let t: u32 = nt[1].parse().unwrap();

    let mut s = String::new();
    io::stdin().read_line(&mut s).unwrap();
    let s = s.trim();

    let vec: Vec<u32> = s.split(" ").map(|n| n.parse().unwrap()).collect();
    let mut result = 0;
    for n in 0..vec.len() {
        if n != 0 {
            let d = vec[n] - vec[n - 1];
            result += if d < t { d } else { t }
        } else {
            result += t;
        }
    }
    println!("{}", result);
}