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

Score : 300 points

Problem Statement

Given are N integers A_1,\ldots,A_N.

Find the sum of A_i \times A_j over all pairs (i,j) such that 1\leq i < j \leq N, modulo (10^9+7).

Constraints

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

0 \leq A_i \leq 10^9

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
A_1 \ldots A_N

Output

Print \sum_{i=1}^{N-1}\sum_{j=i+1}^{N} A_i A_j, modulo (10^9+7).

Sample Input 1

3
1 2 3

Sample Output 1

11

We have 1 \times 2 + 1 \times 3 + 2 \times 3 = 11.

Sample Input 2

4
141421356 17320508 22360679 244949

Sample Output 2

437235829

Submitted source-code file size, in bytes.

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

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


use strict;
use warnings;
use utf8;

use feature qw(say);
use Data::Dumper;

my $k = <STDIN>;
chomp $k;
my ($h, $w, $m) = split / /, $k;

my $h_list = [];
my $h_count_mapping = +{};
my $w_count_mapping = +{};
while (my $input = <STDIN>) {
    chomp $input;
    last unless $input;
    my ($h_n, $w_n) = split / /, $input;
    $h_count_mapping->{$h_n} = ($h_count_mapping->{$h_n} || 0) + 1;
    $w_count_mapping->{$w_n} = ($w_count_mapping->{$w_n} || 0) + 1;

    push @$h_list, [ $h_n, $w_n, ];
}

my ($max_h, $max_h_keys) = max_in_mapping($h_count_mapping);
my ($max_w, $max_w_keys) = max_in_mapping($w_count_mapping);

my $max_h_mapping = +{map { $_ => 1 } @$max_h_keys};
my $max_w_mapping = +{map { $_ => 1 } @$max_w_keys};

my $duplicated_count = 0;
for my $one (@$h_list) {
    my $one_h = $one->[0];
    my $one_w = $one->[1];

    if ($max_h_mapping->{$one_h} && $max_w_mapping->{$one_w}) {
        $duplicated_count++;
    }
}
my $candidate_count = scalar @$max_h_keys * scalar @$max_w_keys;
my $has_duplicated = $candidate_count - $duplicated_count > 0 ? 0 : 1;
my $total_count = $max_h + $max_w - $has_duplicated;
say $total_count;

sub max_in_mapping {
    my $mapping = shift;

    my $max = 0;
    my $max_keys;

    for my $key (keys %$mapping) {
        my $count = $mapping->{$key};
        my $diff = $count - $max;
        if ($diff > 0) {
            $max = $count;
            $max_keys = [$key];
        } elsif ($diff == 0) {
            push @$max_keys, $key;
        }
    }
    return ($max, $max_keys);
}

Score : 500 points

Problem Statement

We have a two-dimensional grid with H \times W squares. There are M targets to destroy in this grid - the position of the i-th target is \left(h_i, w_i \right).

Takahashi will choose one square in this grid, place a bomb there, and ignite it. The bomb will destroy all targets that are in the row or the column where the bomb is placed. It is possible to place the bomb at a square with a target.

Takahashi is trying to maximize the number of targets to destroy. Find the maximum number of targets that can be destroyed.

Constraints

All values in input are integers.

1 \leq H, W \leq 3 \times 10^5

1 \leq M \leq \min\left(H\times W, 3 \times 10^5\right)

1 \leq h_i \leq H

1 \leq w_i \leq W

\left(h_i, w_i\right) \neq \left(h_j, w_j\right) \left(i \neq j\right)

Input

Input is given from Standard Input in the following format:

H W M
h_1 w_1
\vdots
h_M w_M

Output

Print the answer.

Sample Input 1

2 3 3
2 2
1 1
1 3

Sample Output 1

3

We can destroy all the targets by placing the bomb at \left(1, 2\right).

Sample Input 2

3 3 4
3 3
3 1
1 1
1 2

Sample Output 2

3

Sample Input 3

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

Sample Output 3

6


use strict;
use warnings;
use utf8;

use feature qw(say);
use Data::Dumper;

my $k = <STDIN>;
chomp $k;
my ($h, $w, $m) = split / /, $k;

my $h_list = [];
my $h_count_mapping = +{};
my $w_count_mapping = +{};
while (my $input = <STDIN>) {
    chomp $input;
    last unless $input;
    my ($h_n, $w_n) = split / /, $input;
    $h_count_mapping->{$h_n} = ($h_count_mapping->{$h_n} || 0) + 1;
    $w_count_mapping->{$w_n} = ($w_count_mapping->{$w_n} || 0) + 1;

    push @$h_list, [ $h_n, $w_n, ];
}

my ($max_h, $max_h_keys) = max_in_mapping($h_count_mapping);
my ($max_w, $max_w_keys) = max_in_mapping($w_count_mapping);

my $has_duplicated = 1;
for my $h_key (@$max_h_keys) {
    my $target_weights = +{map { $_->[1] => 1 } grep { $_->[0] == $h_key } @$h_list};

    for my $w_key (@$max_w_keys) {
        unless ($target_weights->{$w_key}) {
            $has_duplicated = 0;
            last;
        }
    }
    if ($has_duplicated == 0) {
        last;
    }
}
my $total_count = $max_h + $max_w - $has_duplicated;
say $total_count;

sub max_in_mapping {
    my $mapping = shift;

    my $max = 0;
    my $max_keys;

    for my $key (keys %$mapping) {
        my $count = $mapping->{$key};
        my $diff = $count - $max;
        if ($diff > 0) {
            $max = $count;
            $max_keys = [$key];
        } elsif ($diff == 0) {
            push @$max_keys, $key;
        }
    }
    return ($max, $max_keys);
}


use strict;
use warnings;
use utf8;

use feature qw(say);
use Data::Dumper;

my $k = <STDIN>;
chomp $k;
my ($h, $w, $m) = split / /, $k;

my $h_list = [];
my $h_count_mapping = +{};
my $w_count_mapping = +{};
while (my $input = <STDIN>) {
    chomp $input;
    last unless $input;
    my ($h_n, $w_n) = split / /, $input;
    $h_count_mapping->{$h_n} = ($h_count_mapping->{$h_n} || 0) + 1;
    $w_count_mapping->{$w_n} = ($w_count_mapping->{$w_n} || 0) + 1;

    push @$h_list, +{
        height => $h_n,
        weight => $w_n,
    };
}

my ($max_h, $max_h_keys) = max_in_mapping($h_count_mapping);
my ($max_w, $max_w_keys) = max_in_mapping($w_count_mapping);

my $has_duplicated = 1;
for my $h_key (@$max_h_keys) {
    my $target_weights = +{map { $_->{weight} => 1 } grep { $_->{height} == $h_key } @$h_list};

    for my $w_key (@$max_w_keys) {
        unless ($target_weights->{$w_key}) {
            $has_duplicated = 0;
            last;
        }
    }
    if ($has_duplicated == 0) {
        last;
    }
}
my $total_count = $max_h + $max_w - $has_duplicated;
say $total_count;

sub max_in_mapping {
    my $mapping = shift;

    my $max = 0;
    my $max_keys;

    for my $key (keys %$mapping) {
        my $count = $mapping->{$key};
        my $diff = $count - $max;
        if ($diff > 0) {
            $max = $count;
            $max_keys = [$key];
        } elsif ($diff == 0) {
            push @$max_keys, $key;
        }
    }
    return ($max, $max_keys);
}


use strict;
use warnings;
use utf8;

use feature qw(say);
use Data::Dumper;

my $k = <STDIN>;
chomp $k;
my ($h, $w, $m) = split / /, $k;

my $h_list = [];
my $h_count_mapping = +{};
my $w_count_mapping = +{};
while (my $input = <STDIN>) {
    chomp $input;
    last unless $input;
    my ($h_n, $w_n) = split / /, $input;
    $h_count_mapping->{$h_n} = ($h_count_mapping->{$h_n} || 0) + 1;
    $w_count_mapping->{$w_n} = ($w_count_mapping->{$w_n} || 0) + 1;

    push @$h_list, +{
        height => $h_n,
        weight => $w_n,
    };
}

my ($max_h, $max_h_keys) = max_in_mapping($h_count_mapping);
my ($max_w, $max_w_keys) = max_in_mapping($w_count_mapping);

my $has_duplicated = 1;
for my $h_key (@$max_h_keys) {
    for my $w_key (@$max_w_keys) {
        unless (scalar grep { $_->{height} == $h_key && $_->{weight} == $w_key } @$h_list) {
            $has_duplicated = 0;
            last;
        }
    }
    if ($has_duplicated == 0) {
        last;
    }
}
my $total_count = $max_h + $max_w - $has_duplicated;
say $total_count;

sub max_in_mapping {
    my $mapping = shift;

    my $max = 0;
    my $max_keys;

    for my $key (keys %$mapping) {
        my $count = $mapping->{$key};
        my $diff = $count - $max;
        if ($diff > 0) {
            $max = $count;
            $max_keys = [$key];
        } elsif ($diff == 0) {
            push @$max_keys, $key;
        }
    }
    return ($max, $max_keys);
}


Score : 100 points

Problem Statement

We will buy a product for N yen (the currency of Japan) at a shop.

If we use only 1000-yen bills to pay the price, how much change will we receive?

Assume we use the minimum number of bills required.

Constraints

1 \leq N \leq 10000

N is an integer.

Input

Input is given from Standard Input in the following format:

N

Output

Print the amount of change as an integer.

Sample Input 1

1900

Sample Output 1

100

We will use two 1000-yen bills to pay the price and receive 100 yen in change.

Sample Input 2

3000

Sample Output 2

0

We can pay the exact price.


use strict;
use warnings;
use utf8;

my $input = <STDIN>;
chomp $input;

my $extra = $input % 1000;
my $change = $extra ? 1000 - $extra : 0;
print "$change\n";


use strict;
use warnings;
use utf8;

my $input = <STDIN>;
chomp $input;

my $num = int($input/1000) + 1;
my $change = $num * 1000 - $input;
print "$change\n";


use POSIX qw( ceil );
my $n = int(<>);
print ceil($n / 1000) * 1000 - $n;


use POSIX qw( ceil );
my $n = int(shift());
print ceil($n / 1000) * 1000 - $n;


#!/usr/bin/perl

# 引数取り込み
my $a = <>;

# 処理開始
my $b = 1000 - $a % 1000;

print "$b";

1;


Problem Statement

AtCoder currently hosts three types of contests: ABC, ARC, and AGC. As the number of users has grown, in order to meet the needs of more users, AtCoder has decided to increase the number of contests to 26 types, from AAC to AZC. For convenience, we number these 26 types as type 1 through type 26. AtCoder wants to schedule contests for D days so that user satisfaction is as high as possible. For every day, AtCoder will hold exactly one contest, and each contest will end on that day. The satisfaction is calculated as follows.

The satisfaction at the beginning of day 1 is 0. Satisfaction can be negative.

Holding contests increases satisfaction. The amount of increase will vary depending on a variety of factors. Specifically, we know in advance that holding a contest of type i on day d will increase the satisfaction by s_{d,i}.

If a particular type of contest is not held for a while, the satisfaction decreases. Each contest type i has an integer c_i, and at the end of each day d=1,2,...,D, the satisfaction decreases as follows. Let \mathrm{last}(d,i) be the last day before day d (including d) on which a contest of type i was held. If contests of type i have never been held yet, we define \mathrm{last}(d,i)=0. At the end of day d, the satisfaction decreases by \sum _{i=1}^{26}c_i \times (d-\mathrm{last}(d,i)).

Please schedule contests on behalf of AtCoder.
If the satisfaction at the end of day D is S, you will get a score of \max(10^6 + S, 0).
There are 50 test cases, and the score of a submission is the total scores for each test case.
You can make submissions multiple times, and the highest score among your submissions will be your score.

Constraints

D = 365

Each c_i is an integer satisfying 0\leq c_i \leq 100.

Each s_{d,i} is an integer satisfying 0\leq s_{d,i} \leq 20000.

Input

Input is given from Standard Input in the following format:

D
c_1 c_2 \cdots c_{26}
s_{1,1} s_{1,2} \cdots s_{1,26}
\vdots
s_{D,1} s_{D,2} \cdots s_{D,26}

Output

Let t_d (1\leq t_d \leq 26) be the type of the contest that will be held at day d.
Print D integers t_d to Standard Output in the following format:

t_1
t_2
\vdots
t_D

Any output that does not follow the above format may result in 0 pointsWA for that test case.

Input Generation

Each integer c_i and s_{d,i} is generated independently and uniformly at random from the integers in the range described in the problem statement.

Sample Input 1

5
86 90 69 51 2 96 71 47 88 34 45 46 89 34 31 38 97 84 41 80 14 4 50 83 7 82
19771 12979 18912 10432 10544 12928 13403 3047 10527 9740 8100 92 2856 14730 1396 15905 6534 4650 11469 3628 8433 2994 10899 16396 18355 11424
6674 17707 13855 16407 12232 2886 11908 1705 5000 1537 10440 10711 4917 10770 17272 15364 19277 18094 3929 3705 7169 6159 18683 15410 9092 4570
6878 4239 19925 1799 375 9563 3445 5658 19857 11401 6997 6498 19933 3848 2426 2146 19745 16880 17773 18359 3921 14172 16730 11157 5439 256
8633 15862 15303 10749 18499 7792 10317 5901 9395 11433 3514 3959 5202 19850 19469 9790 5653 784 18500 10552 17975 16615 7852 197 8471 7452
19855 17918 7990 10572 4333 438 9140 9104 12622 4985 12319 4028 19922 12132 16259 17476 2976 547 19195 19830 16285 4806 4471 9457 2864 2192

Sample Output 1

1
17
13
14
13

Note that this example is a small one for checking the problem specification. It does not satisfy the constraint D=365 and is never actually given as a test case. The final satisfaction with this output is 79325, so the score is 1079325.

Input generator, score calculator, and visualizer

Beginner's Guide

If you don't know what to do, proceed to problem B or C.


use List::Util qw(max min sum);
use Time::HiRes qw(gettimeofday tv_interval);

my ($ssec, $smsec) = gettimeofday();

my ($d) = split(/\s+/, <>);
my (@c) = (-1, split(/\s+/, <>));

my @s = ();
for my $i (1..$d) {
    $s[$i] = [-1, split(/\s+/, <>)];
}

sub score {
    my (@contest) = @_;
    my $ret = 0;
    my @last = (-1); for (1..26) { push @last, 0; }
    my $j = 1;

    for my $i (@contest) {
	$ret += $s[$j][$i];
	$last[$i] = $j;
	for (1..26) {
	    $ret -= $c[$_] * ($j-$last[$_]);
	}
	$j++;
    }
    return $ret;
}

sub max_index {
    my (@list) = @_;
    my $maxi = 0;
    for (my $i=0; $i<scalar(@list); $i++) {
	$maxi = $i if $list[$maxi] < $list[$i];
    }
    return $maxi;
}

sub over_index {
    my ($val, @list) = @_;
    my @ret = ();
    for (my $i=0; $i<scalar(@list); $i++) {
	push @ret, $i if $list[$i] > $val;
    }
    return @ret;
}

my @last = (-1); for (1..26) { push @last, 0; }
my @cont = ();
for my $j (1..$d) {
    my $max = max(@{$s[$j]});
    my @can = (1..26);
    #my @can = over_index($max*0.9, @{$s[$j]});
    #print "$j: ", join(",", @can), "\n";

    my $index = -1;
    my $maxre = 0;
    for my $i (@can) {
	my $re = $s[$j][$i] + $c[$i] * ($j-$last[$i]);
	if ($maxre < $re) { $maxre=$re; $index = $i; }
    }
    #push @cont, max_index(@{$s[$j]});
    push @cont, $index;
    $last[$index] = $j;
}

my $score = score(@cont);
while (1) {
    my ($sec, $msec) = gettimeofday();
    #print "$score : $sec $msec\n";
    last if ($sec+$msec/1000000 - $ssec-$smsec/1000000 > 1.7);
    my @new = @cont;
    $new[int(1+rand($d))] = int(1+rand(26));
    my $new_score = score(@new);
    if ($new_score > $score) {
	$score = $new_score;
	@cont = @new;
    }
}

print join("\n", @cont), "\n";
#print score(@cont), "\n";


use List::Util qw(max min sum);

my ($d) = split(/\s+/, <>);
my (@c) = (-1, split(/\s+/, <>));

my @s = ();
for my $i (1..$d) {
    $s[$i] = [-1, split(/\s+/, <>)];
}

sub score {
    my (@contest) = @_;
    my $ret = 0;
    my @last = (-1); for (1..26) { push @last, 0; }
    my $j = 1;

    for my $i (@contest) {
	$ret += $s[$j][$i];
	$last[$i] = $j;
	for (1..26) {
	    $ret -= $c[$_] * ($j-$last[$_]);
	}
	$j++;
    }
    return $ret;
}

sub max_index {
    my (@list) = @_;
    my $maxi = 0;
    for (my $i=0; $i<scalar(@list); $i++) {
	$maxi = $i if $list[$maxi] < $list[$i];
    }
    return $maxi;
}

sub over_index {
    my ($val, @list) = @_;
    my @ret = ();
    for (my $i=0; $i<scalar(@list); $i++) {
	push @ret, $i if $list[$i] > $val;
    }
    return @ret;
}

my @last = (-1); for (1..26) { push @last, 0; }
my @cont = ();
for my $j (1..$d) {
    my $max = max(@{$s[$j]});
    my @can = (1..26);
    #my @can = over_index($max*0.9, @{$s[$j]});
    #print "$j: ", join(",", @can), "\n";

    my $index = -1;
    my $maxre = 0;
    for my $i (@can) {
	my $re = $s[$j][$i] + $c[$i] * ($j-$last[$i]);
	if ($maxre < $re) { $maxre=$re; $index = $i; }
    }
    #push @cont, max_index(@{$s[$j]});
    push @cont, $index;
    $last[$index] = $j;
}

print join("\n", @cont), "\n";
#print score(@cont), "\n";


use List::Util qw(max min sum);

my ($d) = split(/\s+/, <>);
my (@c) = (-1, split(/\s+/, <>));

my @s = ();
for my $i (1..$d) {
    $s[$i] = [-1, split(/\s+/, <>)];
}

sub score {
    my (@contest) = @_;
    my $ret = 0;
    my @last = (-1); for (1..26) { push @last, 0; }
    my $j = 1;

    for my $i (@contest) {
	$ret += $s[$j][$i];
	$last[$i] = $j;
	for (1..26) {
	    $ret -= $c[$_] * ($j-$last[$_]);
	}
	$j++;
    }
    return $ret;
}

sub max_index {
    my (@list) = @_;
    my $maxi = 0;
    for (my $i=0; $i<scalar(@list); $i++) {
	$maxi = $i if $list[$maxi] < $list[$i];
    }
    return $maxi;
}

sub over_index {
    my ($val, @list) = @_;
    my @ret = ();
    for (my $i=0; $i<scalar(@list); $i++) {
	push @ret, $i if $list[$i] > $val;
    }
    return @ret;
}

my @last = (-1); for (1..26) { push @last, 0; }
my @cont = ();
for my $j (1..$d) {
    my $max = max(@{$s[$j]});
    my @can = over_index($max*0.9, @{$s[$j]});
    #print "$j: ", join(",", @can), "\n";

    my $index = -1;
    my $maxre = 0;
    for my $i (@can) {
	my $re = $s[$j][$i] + $c[$i] * ($j-$last[$i]);
	if ($maxre < $re) { $maxre=$re; $index = $i; }
    }
    #push @cont, max_index(@{$s[$j]});
    push @cont, $index;
    $last[$index] = $j;
}

print join("\n", @cont), "\n";
#print score(@cont), "\n";


Score : 300 points

Problem Statement

We have two desks: A and B. Desk A has a vertical stack of N books on it, and Desk B similarly has M books on it.

It takes us A_i minutes to read the i-th book from the top on Desk A (1 \leq i \leq N), and B_i minutes to read the i-th book from the top on Desk B (1 \leq i \leq M).

Consider the following action:

Choose a desk with a book remaining, read the topmost book on that desk, and remove it from the desk.

How many books can we read at most by repeating this action so that it takes us at most K minutes in total? We ignore the time it takes to do anything other than reading.

Constraints

1 \leq N, M \leq 200000

1 \leq K \leq 10^9

1 \leq A_i, B_i \leq 10^9

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N M K
A_1 A_2 \ldots A_N
B_1 B_2 \ldots B_M

Output

Print an integer representing the maximum number of books that can be read.

Sample Input 1

3 4 240
60 90 120
80 150 80 150

Sample Output 1

3

In this case, it takes us 60, 90, 120 minutes to read the 1-st, 2-nd, 3-rd books from the top on Desk A, and 80, 150, 80, 150 minutes to read the 1-st, 2-nd, 3-rd, 4-th books from the top on Desk B, respectively.

We can read three books in 230 minutes, as shown below, and this is the maximum number of books we can read within 240 minutes.

Read the topmost book on Desk A in 60 minutes, and remove that book from the desk.

Read the topmost book on Desk B in 80 minutes, and remove that book from the desk.

Read the topmost book on Desk A in 90 minutes, and remove that book from the desk.

Sample Input 2

3 4 730
60 90 120
80 150 80 150

Sample Output 2

7

Sample Input 3

5 4 1
1000000000 1000000000 1000000000 1000000000 1000000000
1000000000 1000000000 1000000000 1000000000

Sample Output 3

0

Watch out for integer overflows.


use strict;
my ($N,$M,$K) = split(/ /,<>);
my $s = <>; chomp $s;
my @DBA = split(/ /,$s);
my $t = <>; chomp $t;
my @DBB = split(/ /,$t);

my @TA;
my $max=0;
my $ttime=0;
my $A=0;
$TA[0]=0;
for(my $i=0;$i<=$#DBA;$i++){
    $TA[$i+1] = $TA[$i] + $DBA[$i];
    if($TA[$i+1] >$K ){
        last;
    }
    $max++;
}
$A = $max;
$ttime = $TA[$A];
#print "min$min t$ttime\n";
my $read = $max;

for(my $i=0;$i<=$#DBB;$i++){
    #print "$i max$max t$ttime\n";
    my $mod = $K - $ttime;
    my $flag=0;
    while(){
        if($mod >= $DBB[$i] ){last;}
        if($A < 0){
            $flag =1;
            last;
        }
        $read--;
        $A--;
        $mod = $mod + $DBA[$A];
        $ttime = $ttime - $DBA[$A];
    }
    if($flag){last;}
    $read++;
    $ttime = $ttime + $DBB[$i];
    if($read > $max){ $max =$read;}
    #print "max$max t$ttime\n";
}

print "$max";


use strict;
use warnings;
use List::Util qw/ sum max/;

my ($n, $m, $k) = glob <>;
my @a = glob <>;
my @b = glob <>;

my @sum_a = map { sum @a[0..$_] } (0.. $n-1);
my @sum_b = map { sum @b[0..$_] } (0.. $m-1);
my $max_a = scalar grep { $_ <= $k } @sum_a;
my $max_b = scalar grep { $_ <= $k } @sum_b;

my $max = max($max_a, $max_b);
my $fg = 0;

for(my $a_i = $max_a-1; $a_i>= 0; --$a_i) {
    for(my $b_i = $max_b-1; $b_i>= 0; --$b_i) {
        if($k >= $sum_a[$a_i] + $sum_b[$b_i]) {
            $max = $a_i + $b_i + 2;
            $fg = 1;
            last;
        }
    }
    last if ($fg);
}

$fg = 0;

for(my $b_i = $max_b-1; $b_i>= 0; --$b_i) {
    for(my $a_i = $max_a-1; $a_i>= 0; --$a_i) {
        if($k >= $sum_a[$a_i] + $sum_b[$b_i] && $a_i + $b_i + 2 > $max) {
            $max = $a_i + $b_i + 2;
            $fg = 1;
            last;
        }
    }
    last if ($fg);
}

print $max;


my ($N,$M,$K) = split(/ /,<>);
my $s = <>; chomp $s;
my @DBA = split(/ /,$s);
my $t = <>; chomp $t;
my @DBB = split(/ /,$t);

my $min=0;
my $ttime=0;
my $A=0;
for(my $i=0;$i<=$#DBA;$i++){
    $ttime = $ttime + $DBA[$i];
    if($ttime >$K ){
        $ttime = $ttime -$DBA[$i];
        last;
    }
    $min++;
}
$A = $min;
#print "min$min t$ttime\n";

my $read = $min;

for(my $i=0;$i<=$#DBB;$i++){
    #print "$i min$min t$ttime\n";
    my $mod = $K - $ttime;
    my $flag=0;
    while(){
        if($mod >= $DBB[$i] ){last;}
        if($A < 0){
            $flag =1;
            last;
        }
        $read--;
        $A--;
        $mod = $mod + $DBA[$A];
        $ttime = $ttime - $DBA[$A];
    }
    if($flag){last;}
    $read++;
    $ttime = $ttime + $DBB[$i];
    if($read > $min){ $min =$read;}
    #print "min$min t$ttime\n";
}

print "$min";


use strict;
my ($N,$M,$K) = split(/ /,<>);
my $s = <>; chomp $s;
my @DBA = split(/ /,$s);
my $t = <>; chomp $t;
my @DBB = split(/ /,$t);

my $min=0;
my $ttime=0;
my $A=0;
for(my $i=0;$i<=$#DBA;$i++){
    $ttime = $ttime + $DBA[$i];
    if($ttime >$K ){
        $ttime = $ttime -$DBA[$i];
        last;
    }
    $min++;
}
$A = $min;
#print "min$min t$ttime\n";

my $read = $min;

for(my $i=0;$i<=$#DBB;$i++){
    #print "$i min$min t$ttime\n";
    my $mod = $K - $ttime;
    my $flag=0;
    while(){
        if($mod >= $DBB[$i] ){last;}
        if($A < 0){
            $flag =1;
            last;
        }
        $read--;
        $A--;
        $mod = $mod + $DBA[$A];
        $ttime = $ttime - $DBA[$A];
    }
    if($flag){last;}
    $read++;
    $ttime = $ttime + $DBB[$i];
    if($read > $min){ $min =$read;}
    #print "min$min t$ttime\n";
}

print "$min";


Score : 400 points

Problem Statement

For a positive integer X, let f(X) be the number of positive divisors of X.

Given a positive integer N, find \sum_{K=1}^N K\times f(K).

Constraints

1 \leq N \leq 10^7

Input

Input is given from Standard Input in the following format:

N

Output

Print the value \sum_{K=1}^N K\times f(K).

Sample Input 1

4

Sample Output 1

23

We have f(1)=1, f(2)=2, f(3)=2, and f(4)=3, so the answer is 1\times 1 + 2\times 2 + 3\times 2 + 4\times 3 =23.

Sample Input 2

100

Sample Output 2

26879

Sample Input 3

10000000

Sample Output 3

838627288460105

Watch out for overflows.


use strict;
use warnings;
use utf8;

use Data::Dumper;
use feature qw(say);

my $n = <STDIN>;
chomp $n;

my $input = <STDIN>;
chomp $input;
my $array = [split / /, $input];

my $not_divided_array = [];
my $candidate_array = [sort { $a <=> $b } @$array];

while (1) {
    my $candidate = $candidate_array->[0];
    last unless $candidate;

    my $new_candidate_array = [];

    my $has_same = 0;
    for (my $i = 1; $i < scalar @$candidate_array; $i++) {
        my $divided = $candidate_array->[$i];
        if ($divided == $candidate) {
            $has_same = 1;
        } else {
            unless ($divided % $candidate == 0) {
                push @$new_candidate_array, $divided;
            }
        }
    }
    $candidate_array = $new_candidate_array;

    push @$not_divided_array, $candidate unless $has_same;
}
say scalar @$not_divided_array;

Score : 400 points

Problem Statement

Given is a number sequence A of length N.

Find the number of integers i \left(1 \leq i \leq N\right) with the following property:

For every integer j \left(1 \leq j \leq N\right) such that i \neq j , A_j does not divide A_i.

Constraints

All values in input are integers.

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

1 \leq A_i \leq 10^6

Input

Input is given from Standard Input in the following format:

N
A_1 A_2 \cdots A_N

Output

Print the answer.

Sample Input 1

5
24 11 8 3 16

Sample Output 1

3

The integers with the property are 2, 3, and 4.

Sample Input 2

4
5 5 5 5

Sample Output 2

0

Note that there can be multiple equal numbers.

Sample Input 3

10
33 18 45 28 8 19 89 86 2 4

Sample Output 3

5


use strict;
use warnings;
use utf8;

use Data::Dumper;
use feature qw(say);

my $n = <STDIN>;
chomp $n;

my $input = <STDIN>;
chomp $input;
my $array = [split / /, $input];
my $count = 0;

my $sorted_array = [sort { $a <=> $b } @$array];
for (my $i = 0; $i < scalar @$sorted_array; $i++) {
    my $is_divided = 0;
    for (my $divide_index = 0; $divide_index <= $i; $divide_index++) {
        if ($i == $divide_index) {
            next;
        } else {
            my $num = $sorted_array->[$i];
            my $seperator = $sorted_array->[$divide_index];
            if ($num % $seperator == 0) {
                $is_divided = 1
            }
        }
    }
    $count++ unless $is_divided;
}
say $count;


use strict;
use warnings;
use utf8;

use Data::Dumper;
use feature qw(say);

my $n = <STDIN>;
chomp $n;

my $input = <STDIN>;
chomp $input;
my $array = [split / /, $input];
my $count = 0;

for (my $i = 0; $i < scalar @$array; $i++) {
    my $is_divided = 0;
    for (my $divide_index = 0; $divide_index < scalar @$array; $divide_index++) {
        if ($i == $divide_index) {
            next;
        } else {
            my $num = $array->[$i];
            my $seperator = $array->[$divide_index];
            $is_divided = 1 if $num % $seperator == 0;
        }
    }
    $count++ unless $is_divided;
}
say $count;


use strict;
my ($N) = split(/ /,<>);
my $s = <>;chomp $s;
my @dat = split(/ /,$s);
my $MAX = 2*10**5;
#@dat = sort{$a<=>$b}@dat;
my %DB;
for(my $i=0;$i<=$#dat;$i++){
   if( defined($DB{$dat[$i]})){  $DB{$dat[$i]}++;next;}
   $DB{$dat[$i]}=1;
}
foreach my $key ( sort{$a<=>$b} keys %DB){
    if($DB{$key}==0){next;}
    for(my $i=2;$i<=($MAX/$key);$i++){
        if( defined($DB{$key*$i})){
            $DB{$key*$i}=0;
        }
    }
}
my $count =0;
foreach my $key (keys %DB){
    if( $DB{$key} == 1){
        $count++;
    }
}

print "$count";


my $n = <>;
my @sorted_a = sort { $a <=> $b } glob <>;

my @memo = ();

my %counter = ();
for my $cursor (@sorted_a) {
    ++$counter{$cursor};
    if($memo[$cursor] eq 'No') {
        next;
    }
    my $i = 2*$cursor;
    while($sorted_a[$n-1] >= $i) {
        $memo[$i] = 'No';
        $i += $cursor
    }
}

my $ans = grep { $memo[$_] ne 'No' && $counter{$_} == 1 } keys %counter;
print $ans;


my $N = 0;
my $cnt = 0;
while(<STDIN>)
{
  chomp $_;
  my @cols = split(" ", $_);
  if($cnt == 0) {
    $N = $cols[0];
    $cnt++;
  }elsif($cnt == 1) {
    my $num = $N;
    my %dup;
    foreach my $val (@cols) {
      if( exists($dup{$val}) ) {
        $dup{$val} += 1;
      }else {
        $dup{$val} = 1;
      }
    }
    my @nrs = keys(%dup);
    my @neos = sort {$b <=> $a} @nrs;
    my $neo_num = @neos;
    for(my $i=0; $i < $neo_num; $i++) {
      my $val = $neos[$i];
      if($dup{$val} > 1) {
        $num = $num - $dup{$val};
      }elsif($i == ($neo_num-1)) {
      }else {
        for(my $j=($i+1); $j < $neo_num; $j++) {
          my $cur = $val % $neos[$j];
          if( $cur == 0 ) {
            $num--;
            last;
          }
        }
      }
    }
    print $num;
  }
}


my $N = 0;
my $cnt = 0;
while(<STDIN>)
{
  chomp $_;
  my @cols = split(" ", $_);
  if($cnt == 0) {
    $N = $cols[0];
    $cnt++;
  }elsif($cnt == 1) {
    my @vals = sort {$b <=> $a} @cols;
    my $num = $N;
    my %dup;
    foreach my $val (@vals) {
      if( exists($dup{$val}) ) {
        $dup{$val} += 1;
      }else {
        $dup{$val} = 1;
      }
    }
    my @nrs = keys(%dup);
    my @neos = sort {$b <=> $a} @nrs;
    my $neo_num = @neos;
    for(my $i=0; $i < $neo_num; $i++) {
      my $val = $neos[$i];
      if($dup{$val} > 1) {
        $num = $num - $dup{$val};
      }elsif($i == ($neo_num-1)) {
      }else {
        for(my $j=($i+1); $j < $neo_num; $j++) {
          my $cur = $val % $neos[$j];
          if( $cur == 0 ) {
            $num--;
            last;
          }
        }
      }
    }
    print $num;
  }
}


my $n = <>;
my @A = glob <>;
my @sorted_a = sort { $a <=> $b } @A;

my @memo = ();

my $ans = 0;
for my $cursor (0..$#sorted_a) {
    if($memo[$cursor] eq 'No') {
        next;
    }
    for (grep { $sorted_a[$_] % $sorted_a[$cursor] == 0 } ($cursor+1 .. $#sorted_a)) {
        $memo[$_] = 'No';
    };
    if($cursor != $#sorted_a && $sorted_a[$cursor] == $sorted_a[$cursor+1]) {
        next;
    }
    ++$ans;
}

print $ans;


my $N = 0;
my $cnt = 0;
while(<STDIN>)
{
  chomp $_;
  my @cols = split(" ", $_);
  if($cnt == 0) {
    $N = $cols[0];
    $cnt++;
  }elsif($cnt == 1) {
    my @vals = sort {$b <=> $a} @cols;
    my $num = $N;
    my %dup;
    foreach my $val (@vals) {
      if( exists($dup{$val}) ) {
        $dup{$val} += 1;
      }else {
        $dup{$val} = 1;
      }
    }
    for(my $i=0; $i < ($N-1); $i++) {
      my $val = $vals[$i];
      if($dup{$val} > 1) {
        $num--;
      }else {
        for(my $j=($i+1); $j < $N; $j++) {
          my $cur = $val % $vals[$j];
          if( $cur == 0 ) {
            $num--;
            last;
          }
        }
      }
    }
    if(($vals[$N-1] % $vals[$N-2]) == 0) {
      $num--;
    }
    print $num;
  }
}


use strict;
my ($N) = split(/ /,<>);
my $s = <>;chomp $s;
my @dat = split(/ /,$s);
@dat = sort{$a<=>$b}@dat;
my %DB;
for(my $i=0;$i<=$#dat;$i++){
  #print "$dat[$i]\n";
   my $flag =1;
   if( $dat[$i]==$dat[$i+1] ){ $DB{$dat[$i]}=2;next;}
   if( defined($DB{$dat[$i]})){  $DB{$dat[$i]}=2;next;}
   foreach my $key (sort keys %DB){
      if( $dat[$i]%$key == 0){
        $flag=0;
        last;
      }
   }
   if($flag){
     $DB{$dat[$i]}=1;
   }
}
my $count =0;
foreach my $key (keys %DB){
    if( $DB{$key} == 1){
        $count++;
    }
}
print "$count";


use strict;
my ($N) = split(/ /,<>);
my $s = <>;chomp $s;
my @dat = split(/ /,$s);
@dat = sort{$a<=>$b}@dat;
my %DB;
for(my $i=0;$i<=$#dat;$i++){
  #print "$dat[$i]\n";
   my $flag =1;
   if( defined($DB{$dat[$i]})){  $DB{$dat[$i]}++;next;}
   foreach my $key (sort {$a <=> $b} keys %DB){
      if( $dat[$i]%$key == 0){
        $flag=0;
        last;
      }
   }
   if($flag){
     $DB{$dat[$i]}=1;
   }
}
my $count =0;
foreach my $key (keys %DB){
    if( $DB{$key} == 1){
        $count++;
    }
}
print "$count";


my $n = <>;
my @A = glob <>;
my @sorted_a = sort { $a <=> $b } @A;

my $ans = 0;
for my $cursor (0..$#sorted_a) {
    if($cursor != $#sorted_a && $sorted_a[$cursor] == $sorted_a[$cursor+1]) {
        next;
    }
    ++$ans;
    for (0..$cursor-1) {
        if($sorted_a[$cursor] % $sorted_a[$_] == 0) {
            --$ans;
            last;
        }
    }
}

print $ans;


use strict;
my ($N) = split(/ /,<>);
my $s = <>;chomp $s;
my @dat = split(/ /,$s);
my %DB;
for(my $i=0;$i<=$#dat;$i++){
  if( defined($DB{$dat[$i]})){  $DB{$dat[$i]}++;next;}
  $DB{$dat[$i]}=1;
}
my %DB2;

foreach my $num ( keys %DB){
  #print "$num\n";
   my $flag =1;
   if( defined($DB2{$num})){  $DB2{$num}++;next;}
   foreach my $key (keys %DB2){
      if( $num%$key == 0){
        $flag=0;
        last;
      }
   }
   if($flag){
     $DB2{$num}=$DB{$num};
   }
}
my $count =0;
foreach my $key (keys %DB2){
    if( $DB2{$key} == 1){
        $count++;
    }
}
print "$count";


use strict;
my ($N) = split(/ /,<>);
my $s = <>;chomp $s;
my @dat = split(/ /,$s);
@dat = sort{$a<=>$b}@dat;
my %DB;

for(my $i=0;$i<=$#dat;$i++){
  #print "$dat[$i]\n";
   my $flag =1;
   if( defined($DB{$dat[$i]})){  $DB{$dat[$i]}++;next;}
   foreach my $key (keys %DB){
      if( $dat[$i]%$key == 0){
        $flag=0;
        last;
      }
   }
   if($flag){
     $DB{$dat[$i]}=1;
   }
}
my $count =0;
foreach my $key (keys %DB){
    if( $DB{$key} == 1){
        $count++;
    }
}
print "$count";


use integer;
$n=<>;
$ans=1;
$limit=10**18;
@a=glob<>;
if(grep{$_==0}@a){
	print 0;
	exit
}
map{
	if($ans>$limit/$_){
		print -1;
		exit
	}
	$ans*=$_;
}@a;
print $ans;

Score : 200 points

Problem Statement

Given N integers A_1, ..., A_N, compute A_1 \times ... \times A_N.

However, if the result exceeds 10^{18}, print -1 instead.

Constraints

2 \leq N \leq 10^5

0 \leq A_i \leq 10^{18}

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
A_1 ... A_N

Output

Print the value A_1 \times ... \times A_N as an integer, or -1 if the value exceeds 10^{18}.

Sample Input 1

2
1000000000 1000000000

Sample Output 1

1000000000000000000

We have 1000000000 \times 1000000000 = 1000000000000000000.

Sample Input 2

3
101 9901 999999000001

Sample Output 2

-1

We have 101 \times 9901 \times 999999000001 = 1000000000000000001, which exceeds 10^{18}, so we should print -1 instead.

Sample Input 3

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

Sample Output 3

0


use integer;
$n=<>;
$ans=1;
$limit=10**18;
@a=glob<>;
if(grep{!$_}@a){
	print 0;
	exit
}
map{
	$ans*=$_;
	if($ans>$limit){
		print -1;
		exit
	}
}@a;
print $ans;


# Your code here!
use strict;
use warnings;
#use bignum;

my $N = <STDIN>;
my @a = split(/ /, <STDIN>);
chomp($a[$N-1]);
print(calc());

sub calc
{
    for( my $i=0; $i < $N; $i ++ )
    {

        if( $a[$i] == 0 )
        {
            return 0;
        }
    }

    my $val = 1;
    for( my $i=0; $i < $N; $i ++ )
    {

        if( 1e18 / $a[$i] < $val )
        {
            return -1;
        }

        $val *= $a[$i];
    }

    if( 1e18 < $val )
    {
        return -1;
    }

    return $val;
}


# Your code here!
use strict;
use warnings;
use bignum;

my $N = <STDIN>;
my @a = split(/ /, <STDIN>);
chomp($a[$N-1]);
print(calc());

sub calc
{
    for( my $i=0; $i < $N; $i ++ )
    {

        if( $a[$i] == 0 )
        {
            return 0;
        }
    }

    my $val = 1;
    for( my $i=0; $i < $N; $i ++ )
    {

        if( 1e18 / $a[$i] < $val )
        {
            return -1;
        }

        $val *= $a[$i];
    }

    if( 1e18 < $val )
    {
        return -1;
    }

    return $val;
}


# Your code here!
use strict;
use warnings;
use bignum;

my $N = <STDIN>;
my @a = split(/ /, <STDIN>);
chomp($a[$N-1]);
print(calc());

sub calc
{
    my $val = 1;
    for( my $i=0; $i < $N; $i ++ )
    {
        if( $a[$i] == 0 )
        {
            return 0;
        }

        if( 1e18 / $a[$i] < $val )
        {
            return -1;
        }

        $val *= $a[$i];
    }

    if( 1e18 < $val )
    {
        return -1;
    }

    return $val;
}


# Your code here!
use strict;
use warnings;
use bignum;

my $N = <STDIN>;
my @a = split(/ /, <STDIN>);
chomp($a[$N-1]);
print(calc());

sub calc
{
    my $val = 1;
    for( my $i=0; $i < $N; $i ++ )
    {
        if( 1e18 / $a[$i] < $val )
        {
            return -1;
        }

        if( $a[$i] == 0 )
        {
            return 0;
        }
        $val *= $a[$i];
    }

    if( 1e18 < $val )
    {
        return -1;
    }

    return $val;
}


my $n_tmp = get;
my $n = chomp($n_tmp);

my $in = get;
my $input = chomp($in);

my @data = split(' ', $input);
my $ans = 1;

for @data -> $l {
	if $l == 0 {
		say '0';
		exit();
	}
}

for @data -> $i {
	if $i <= 1000000000000000000/$ans {
		$ans = $ans * $i;
	} else {
		say '-1';
		exit();
	}

}

say $ans


use strict;
use warnings;
use bignum;
use feature 'say';

my $line = <STDIN>;

my $border = 1000000000000000000;
my @nums = split /\s/, <STDIN>;
my $has_zero = grep { $_ == 0 } @nums;

if ($has_zero) {
  say 0;
  exit;
}

my $ret = 1;
for my $num (@nums) {
  $ret *= $num;
  if($ret > $border) {
    say '-1';
    exit;
  }
}

say $ret;


use strict;
use warnings;
use bignum;
use feature 'say';

my $line = <STDIN>;

my $border = 10 ** 18;
my @nums = split /\s/, <STDIN>;
my $has_zero = grep { $_ == 0 } @nums;

if ($has_zero) {
  say 0;
  exit;
}

my $ret = 1;
map { $ret *= $_ } @nums;

if ($ret <= $border) {
  say $ret;
} else {
  say '-1';
}


use strict;
use warnings;
use bignum;
use feature 'say';

my $line = <STDIN>;

my $border = 10 ** 18;
my @nums = split /\s/, <STDIN>;
my $has_zero = grep { $_ == 0 } @nums;

if ($has_zero) {
  say 0;
  exit;
}

my $ret = 1;

for my $num (@nums) {
  $ret *= $num;
}
if ($ret <= $border) {
  say $ret;
} else {
  say '-1';
}


use strict;
use warnings;
use bignum;
use feature 'say';

my $border = 10 ** 18;
chomp(my $in = <STDIN>);
my @nums = split /\s/, $in;
my $ret = 1;
for my $num (@nums) {
  $ret *= $num;
}
if ($ret <= $border) {
  say $ret;
} else {
  say '-1';
}


# Your code here!
use strict;
use warnings;

my ($a,$b) = split(/ /, <STDIN>);
my $c = int($b * 100.001);
print(int($a*$b/100));

Score : 300 points

Problem Statement

Compute A \times B, truncate its fractional part, and print the result as an integer.

Constraints

0 \leq A \leq 10^{15}

0 \leq B < 10

A is an integer.

B is a number with two digits after the decimal point.

Input

Input is given from Standard Input in the following format:

A B

Output

Print the answer as an integer.

Sample Input 1

198 1.10

Sample Output 1

217

We have 198 \times 1.10 = 217.8. After truncating the fractional part, we have the answer: 217.

Sample Input 2

1 0.01

Sample Output 2

0

Sample Input 3

1000000000000000 9.99

Sample Output 3

9990000000000000


# Your code here!
use strict;
use warnings;

my ($a,$b) = split(/ /, <STDIN>);
my $c = $b*100 + 1e-1;
print(int(($a*int($c))/100));


# Your code here!
use strict;
use warnings;

my ($a,$b) = split(/ /, <STDIN>);
my $c = $b*100 + 1e-1;
print(int(($a*$c)/100));


# Your code here!
use strict;
use warnings;

my ($a,$b) = split(/ /, <STDIN>);
print(int($a*$b));


use POSIX qw(floor);
my ($a, $b) = glob <>;
=pod
my $ans = $a * ($b * 100);
$ans = $ans - ($ans % 100);
$ans = $ans / 100;
print("$ans");
=cut

my $ans = $a * ($b * 100);
printf("%d", floor($ans / 100));


my $s = <> ;chomp $s;
my ($A,$B) = split(/\s+/, $s);
my $ans;
my $mod;

if($A > 100){
$mod = substr($A,-2);
$mod = int(($mod * $B));
#print "$mod\n";
my $AA = substr($A,0,-2);
#print "AA$AA\n";
$B= int($B*100);
$ans = $AA * $B;
$ans = $ans + $mod;
}else{
  $ans = int ($A * $B );
}
print "$ans";


my $s = <> ;chomp $s;
my ($A,$B) = split(/\s+/, $s);
my $ans;
my $mod;

if($A > 100){

$mod = substr($A,-2);
#print "$mod\n";
my $AA = substr($A,0,-2);
#print "AA$AA\n";
$B= int($B*100);
$ans = $AA * $B;
$mod = int(($mod * $B)/100);
$ans = $ans + $mod;
}else{
  $ans = int ($A * $B );
}
print "$ans";


my $s = <> ;chomp $s;
my ($A,$B) = split(/\s+/, $s);
my $ans;

my $mod = $A%100;

$mod = substr($A,-2);
#print "$mod\n";
my $AA = substr($A,0,-2);
#print "AA$AA\n";
$B= int($B*100);
$ans = $AA * $B;

$mod = int(($mod * $B)/100);
$ans = $ans + $mod;

my $str = $ans;
if($ans==0){print "0";exit;}
chop $str;
chop $str;

print "$ans";


use strict;
use warnings;
use POSIX;

chomp(my $in = <STDIN>);
my ($a, $b) = split /\s/, $in;
my $multi = $a * $b;
my $floor = floor($multi);
printf('%d%s', $floor, "\n");


my $s = <> ;chomp $s;
my ($A,$B) = split(/\s+/, $s);
my $ans;
my $mod = $A%100;
$B= int($B*100);
$A = ($A -$mod)/100;
$ans = $A * $B;

$mod = int(($mod * $B)/100);
$ans = $ans + $mod;

my $str = $ans;
if($ans==0){print "0";exit;}
chop $str;
chop $str;

print "$ans";


use strict;
use warnings;
use POSIX;
use feature 'say';

my ($a, $b) = split /\s/, <STDIN>;
my $multi = $a * $b;
my $floor = floor($multi);
printf('%d', $floor);


use strict;
use warnings;
use POSIX;
use feature 'say';

my ($a, $b) = split /\s/, <STDIN>;
say $a;
say $b;
my $multi = $a * $b;
my $floor = floor($multi);
printf('%d', $floor);


my $s = <> ;chomp $s;
my ($A,$B) = split(/\s+/, $s);
my $ans;
$B= int($B*100);
$ans = $A * $B;
my $str = $ans;
if($ans==0){print "0";exit;}
chop $str;
chop $str;

$ans = $ans ;
$ans = $ans/100;
print "$str";


my $s = <> ;chomp $s;

my ($A,$B) = split(/\s+/, $s);
my $ans;
$B= int($B*100);
$ans = $A * $B;
my $str = $ans;
chop $str;
chop $str;

$ans = $ans ;
$ans = $ans/100;
print "$str";


Score: 400 points

Problem Statement

The Kingdom of Takahashi has N towns, numbered 1 through N.

There is one teleporter in each town. The teleporter in Town i (1 \leq i \leq N) sends you to Town A_i.

Takahashi, the king, loves the positive integer K. The selfish king wonders what town he will be in if he starts at Town 1 and uses a teleporter exactly K times from there.

Help the king by writing a program that answers this question.

Constraints

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

1 \leq A_i \leq N

1 \leq K \leq 10^{18}

Input

Input is given from Standard Input in the following format:

N K
A_1 A_2 \dots A_N

Output

Print the integer representing the town the king will be in if he starts at Town 1 and uses a teleporter exactly K times from there.

Sample Input 1

4 5
3 2 4 1

Sample Output 1

4

If we start at Town 1 and use the teleporter 5 times, our travel will be as follows: 1 \to 3 \to 4 \to 1 \to 3 \to 4.

Sample Input 2

6 727202214173249351
6 5 2 5 3 2

Sample Output 2

2


my $s = <>;
my ($N,$K) = split(/ /,$s);

$s = <>;
my @DB = split (/ /,$s);
my %flag;
my $next = 1;
for(my $i=0;$i<$K;$i++){
    #print "now $next\n";
    if( $flag{$next}< 1 ){
        $flag{$next} = $i;
    }else{
        #print "still came  $i -$flag{$next} \n";
        my $mod =  $i -$flag{$next};
        if( ($K-$i) > $mod ){
          #print "$i + int((($K-$i)-($K-$i)%$mod)/$mod)*$mod\n";
          $i = $i + int((($K-$i)-($K-$i)%$mod)/$mod)*$mod-$mod;
        }
    }
    $next = $DB[$next-1];
    #print "i$i\n";
    #print  "nx $next\n";
}
print "$next"


my ($n,$k) = split(/\s+/, <>);
my (@a) = (0, split(/\s+/, <>));
my @visited = ();

my $c = 1;
my $pos = 1;
while (!$visited[$pos]) {
    $visited[$pos] = $c;
    $pos = $a[$pos];
    $c++;
}

$c -= $visited[$pos];

my $current = 1;
while ($k-->0) {
    $current = $a[$current];
    if ($current == $pos) {
	$k %= $c;
    }
}

print "$current\n";


Score : 200 points

Problem Statement

Takahashi and Aoki will have a battle using their monsters.

The health and strength of Takahashi's monster are A and B, respectively, and those of Aoki's monster are C and D, respectively.

The two monsters will take turns attacking, in the order Takahashi's, Aoki's, Takahashi's, Aoki's, ...
Here, an attack decreases the opponent's health by the value equal to the attacker's strength.
The monsters keep attacking until the health of one monster becomes 0 or below. The person with the monster whose health becomes 0 or below loses, and the other person wins.

If Takahashi will win, print Yes; if he will lose, print No.

Constraints

1 \leq A,B,C,D \leq 100

All values in input are integers.

Input

Input is given from Standard Input in the following format:

A B C D

Output

If Takahashi will win, print Yes; if he will lose, print No.

Sample Input 1

10 9 10 10

Sample Output 1

No

First, Takahashi's monster attacks Aoki's monster, whose health is now 10-9=1.

Next, Aoki's monster attacks Takahashi's monster, whose health is now 10-10=0.

Takahashi's monster is the first to have 0 or less health, so Takahashi loses.

Sample Input 2

46 4 40 5

Sample Output 2

Yes


#! /usr/bin/perl

use strict;
use warnings;

my $in = <>;
chomp($in);
my ($a, $b, $c, $d) = split(/\s+/, $in);

my $n1 = int($c / $b);
$n1 ++ if $c % $b != 0;

my $n2 = int($a / $d);
$n2 ++ if $a % $d != 0;

if($n1 <= $n2) {
    print "Yes\n";
}else{
    print "No\n";
}


#! /usr/bin/perl

use strict;
use warnings;

my $in = <>;
chomp($in);
my ($a, $b, $c, $d) = split(/\s+/, $in);

my $n1 = $c / $b;
$n1 ++ if $c % $b != 0;

my $n2 = $a / $d;
$n2 ++ if $a % $d != 0;

if($n1 <= $n2) {
    print "Yes\n";
}else{
    print "No\n";
}


#! /usr/bin/perl

use strict;
use warnings;

my $in = <>;
chomp($in);
my ($a, $b, $c, $d) = split(/\s+/, $in);

my $n1 = $c / $b;
$n1 ++ if $c % $b != 0;

my $n2 = $a / $d;
$n2 ++ if $a % $d != 0;

if($n1 < $n2) {
    print "Yes\n";
}else{
    print "No\n";
}


use POSIX 'ceil';
my ($a, $b, $c, $d) = glob <>;
print((ceil($c / $b) > ceil($a / $d)) ? 'No' : 'Yes');


use strict;
use warnings;
use utf8;
use feature qw/say/;
use POSIX qw/ceil/;

my $inp = <STDIN>;
chomp($inp);
my ($t_hp, $t_ap, $a_hp, $a_ap) = split(/ /, $inp);

my $t_cnt = ceil($a_hp/$t_ap);
my $a_cnt = ceil($t_hp/$a_ap);

if ($t_cnt <= $a_cnt) {
    say 'Yes';
} else {
    say 'No';
}


use v5.26; # strict say state biwise
use warnings;
use List::Util qw(reduce any all none notall first max maxstr min minstr product sum sum0 pairs unpairs pairkeys pairvalues pairfirst pairgrep pairmap shuffle uniq uniqnum uniqstr);

chomp (my ($a, $b, $c, $d) = split / /, <STDIN>);
my $t = 0;
while ($a > 0 && $c > 0) {
  if ($t % 2 == 0) {
    $c -= $b;
  } else {
    $a -= $d;
  }
  $t++;
}
my $ans = ($a <= 0) ? "No" : "Yes";
say $ans;


use v5.26; # strict say state biwise

chomp (my ($a, $b, $c, $d) = split / /, <STDIN>);
my $t = 0;
while ($a > 0 && $c > 0) {
  if ($t % 2 == 0) {
    $c -= $b;
  } else {
    $a -= $d;
  }
  $t++;
}
my $ans = ($a == 0) ? "No" : "Yes";
say $ans;


Score : 400 points

Problem Statement

Given is a string S consisting of digits from 1 through 9.

Find the number of pairs of integers (i,j) (1 ≤ i ≤ j ≤ |S|) that satisfy the following condition:

Condition: In base ten, the i-th through j-th characters of S form an integer that is a multiple of 2019.

Constraints

1 ≤ |S| ≤ 200000

S is a string consisting of digits from 1 through 9.

Input

Input is given from Standard Input in the following format:

S

Output

Print the number of pairs of integers (i,j) (1 ≤ i ≤ j ≤ |S|) that satisfy the condition.

Sample Input 1

1817181712114

Sample Output 1

3

Three pairs - (1,5), (5,9), and (9,13) - satisfy the condition.

Sample Input 2

14282668646

Sample Output 2

2

Sample Input 3

2119

Sample Output 3

0

No pairs satisfy the condition.


#! /usr/bin/perl

use strict;
use warnings;

my $in = <>;
chomp($in);
my $len = length($in);

# (1,5),(5,9),(9,13)

my %map;
$map{0} = 1;
for(my $i = 0;$i < $len;$i ++){
    #    print substr($in, $i), "\n";
    my $mod = substr($in, $i) % 2019;
#    print "$i $mod\n";
    $map{$mod} ||= 0;
    $map{$mod} ++;
}
#print "\n\n";

my $n = 0;
foreach my $i (keys %map) {
    $n ++ if 1 < $map{$i};
}
print "$n\n";


#! /usr/bin/perl

use strict;
use warnings;

my $in = <>;
my $len = length($in);

my $ok = 0;
for(my $i = 0;$i < $len;$i ++){
    for(my $j = $i + 4;$j < $len;$j ++){
        if(substr($in, $i, $j-$i) % 2019 == 0){
            print $i+1, " ", $j+1, "\n";
            $ok = 1;
        }
    }
}
print "0\n" unless $ok;


use v5.26; # strict say state biwise
use warnings;
use List::Util qw(reduce any all none notall first max maxstr min minstr product sum sum0 pairs unpairs pairkeys pairvalues pairfirst pairgrep pairmap shuffle uniq uniqnum uniqstr);

sub pow {
  my $d = shift;
  if ($d == 0) {
    1;
  } elsif ($d == 1) {
    10;
  } elsif ($d % 2 == 1) {
    (10 * pow($d - 1)) % 2019;
  } else {
    (pow($d / 2) ** 2) % 2019;
  }
}
my @s;
{
  chomp (my $in = <STDIN>);
  @s = split '', $in;
}
my $ans = 0;
my (@dp, @dpmod);
$dp[0] = 1;
for my $i (1..scalar(@s)) {
  my $t = $s[-$i];
  $dpmod[$i] = ($t * pow($i) + $dpmod[$i - 1]) % 2019;
  $ans += $dp[$dpmod[$i]];
  $dp[$dpmod[$i]]++;
}
say $ans;


use Math::BigInt;

$s = Math::BigInt->new($s);
chomp($s = <STDIN>);

$len = length($s);
$cnt = 0;
for ($i = 0; $i <= $len - 4; $i++){
	for ($n = $i + 3; $n <= $len -1; $n++){
		$one = substr($s, $i, $n + 1);
		$cnt ++ if($one % 2019 == 0);
	}
}

print "$cnt\n";


#!/usr/bin/env perl
use 5.10.0;
use utf8;
use strict;
use warnings;
use open qw( :utf8 :std );
use Data::Dumper;

chomp( my $s = readline );
my $length = length $s;

my $founds = 0;

my %map = concrete_map( );
#warn Dumper \%map;

for my $i ( 0..( $length - 1 ) ) {
    for my $j ( $i..( $length - 1 ) ) {
        my $value = substr $s, $i, $j - $i + 1;
        #warn "\$i: $i, \$j, $j, $value";

        $founds++
            if $map{ $value };

        #warn "($i, $j, $value)" if $map{ $value };
    }
}

say $founds;

exit;

sub concrete_map {
    my @values;
    my $value = 2019;

    while ( $value <= 200_000 ) {
        push @values, $value;
        $value = $value + 2019;
    }

    return map { ( $_ => 1 ) } @values;
}


my ($s) = split(/\s+/, <>);

my $ans = 0;

for my $begin (0..length($s)-1) {
    my $rest=substr($s,$begin,3);
    #print "begin=$begin\n";
    for my $pos ($begin+3..length($s)-1) {
	#print "rest=$rest\n";
	$rest .= substr($s, $pos, 1);
	$rest %= 2019;
	$ans++ if $rest==0;
    }
}
print "$ans\n";


use strict;
use warnings;
use utf8;
use feature qw/say/;

sub main {
    my $s = <STDIN>;
    chomp($s);

    if (length($s) <= 3) {
        say 0;
    } else {
        say calc($s);
    }
}

sub calc {
    my ($s) = @_;

    my $cnt = 0;
    for my $i (1..length($s)) {
        for my $j (4..(length($s)-$i+1)) {
            my $target = substr($s, $i-1, $j);
            next unless _sum($target)%3 == 0;
            next unless int($target)%673 == 0;
            $cnt++ if int($target)%2019 == 0;
        }
    }
    return $cnt;
}

sub _sum {
    my $s = shift;
    my $tot;
    for (split(//, $s)) {
        $tot += int($_);
    }
    return $tot;
}

main();


use v5.26; # strict say state biwise

chomp (my $s = <STDIN>);
my $n = length($s) - 1;
my $cnt = 0;
for my $i (0..$n - 4) {
  for my $j (4..$n - $i + 1) {
    my $x = substr($s, $i, $j);
    $cnt++ if $x % 2019 == 0
  }
}
say $cnt;


Score : 100 points

Problem Statement

Print the circumference of a circle of radius R.

Constraints

1 \leq R \leq 100

All values in input are integers.

Input

Input is given from Standard Input in the following format:

R

Output

Print the circumference of the circle.
Your output is considered correct if and only if its absolute or relative error from our answer is at most 10^{-2}.

Sample Input 1

1

Sample Output 1

6.28318530717958623200

Since we accept an absolute or relative error of at most 10^{-2}, 6.28 is also an acceptable output, but 6 is not.

Sample Input 2

73

Sample Output 2

458.67252742410977361942


use v5.26; # strict say state

chomp (my $r = <STDIN>);
say ($r * 6.283185);


use Math::Trig 'pi';
print 2 * pi * <STDIN>;


use v5.18; # strict say state
use warnings;
use List::Util qw(reduce first max min sum0);

chomp (my $r = <STDIN>);
say ($r * 6.28318530717958623200);


#!/usr/bin/perl

# 引数取り込み
my $a = <>;
my $b = substr( $a, 0, 1 );
my $c = substr( $a, 1, 1 );
my $d = substr( $a, 2, 1 );

# 処理開始
if ( $b == 7 or $c == 7 or $d == 7 ) {
    print "Yes";
}
else {
    print "No";
}

1;

Score : 100 points

Problem Statement

Given is a three-digit integer N. Does N contain the digit 7?

If so, print Yes; otherwise, print No.

Constraints

100 \leq N \leq 999

Input

Input is given from Standard Input in the following format:

N

Output

If N contains the digit 7, print Yes; otherwise, print No.

Sample Input 1

117

Sample Output 1

Yes

117 contains 7 as its last digit.

Sample Input 2

123

Sample Output 2

No

123 does not contain the digit 7.

Sample Input 3

777

Sample Output 3

Yes


use strict;
use warnings;
use utf8;

sub any (&@) {
   my $f = shift;
   foreach ( @_ ) {
       return 1 if $f->();
   }
   return 0;
}

print((any { $_ eq '7' } split '',  <STDIN>) ? 'Yes' : 'No');


my $N =<STDIN>;
if ($N=~/7/){print "Yes\n";}else{print "No\n";}


use v5.18; # strict say state
use warnings;
use List::Util qw(reduce first max min sum0);

chomp (my $n = <STDIN>);
if (index($n, '7')==-1) {
  say "No";
} else {
  say "Yes";
}


use strict;
use warnings;

chomp(my $a = <>);

if ($a =~ /7/) {
  print "Yes\n";
} else {
  print "No\n";
}

# print "$a \n";


my $k = Int(get);
my $ans = 0;
my @loop = 1..$k;
for @loop -> $a {
    for @loop -> $b {
        my $tmp = infix:<gcd>($a, $b);
        for @loop -> $c {
            $ans += infix:<gcd>($tmp, $c);
        }
    }
}
print "$ans\n";

Score : 300 points

Problem Statement

Find \displaystyle{\sum_{a=1}^{K}\sum_{b=1}^{K}\sum_{c=1}^{K} \gcd(a,b,c)}.

Here \gcd(a,b,c) denotes the greatest common divisor of a, b, and c.

Constraints

1 \leq K \leq 200

K is an integer.

Input

Input is given from Standard Input in the following format:

K

Output

Print the value of \displaystyle{\sum_{a=1}^{K}\sum_{b=1}^{K}\sum_{c=1}^{K} \gcd(a,b,c)}.

Sample Input 1

2

Sample Output 1

9

\gcd(1,1,1)+\gcd(1,1,2)+\gcd(1,2,1)+\gcd(1,2,2)
+\gcd(2,1,1)+\gcd(2,1,2)+\gcd(2,2,1)+\gcd(2,2,2)
=1+1+1+1+1+1+1+2=9

Thus, the answer is 9.

Sample Input 2

200

Sample Output 2

10813692


my $k = Int(get);
my $ans = 0;
my @loop = 1..$k;
for @loop -> $a {
    for @loop -> $b {
        my $tmp = $a gcd $b;
        for @loop -> $c {
            $ans += $tmp gcd $c;
        }
    }
}
print "$ans\n";


my $N = <STDIN>;
if($N==1){print "$N";exit;}
my $sum =0;
my %del;
for(my $i=$N;$i>0;$i--){
 $count = int($N /$i);
 $del{$i} = ($count*$count*$count);
}
for(my $i=$N;$i>0;$i--){
  $count = int($N /$i);
  if($count >1){
  for(my $j=2;$j<=$count;$j++){
    $del{$i}= $del{$i} - $del{$i*$j};
   }
   }
 $sum = $sum + $i*$del{$i};
 #print "i$i del$del{$i} $sum\n";
}
print "$sum";
exit;


my $N = <STDIN>;
if($N==1){print "$N";exit;}
my $sum =0;
my %del;
for(my $i=$N;$i>0;$i--){
 $count = int($N /$i);
 $del{$i} = ($count*$count*$count);
}
for(my $i=$N;$i>0;$i--){
  $count = int($N /$i);
  for(my $j=2;$j<=$count;$j++){
    $del{$i}= $del{$i} - $del{$i*$j};
 }
 $sum = $sum + $i*$del{$i};
 #print "i$i del$del{$i} $sum\n";
}
print "$sum";


my $N = <STDIN>;
if($N==1){print "$N";exit;}
my $sum =0;
my %del;
for(my $i=$N;$i>0;$i--){
 $count = int($N /$i);
 $del{$i} = ($count*$count*$count);
}
for(my $i=$N;$i>1;$i--){
  $del{1}=$del{1} - $del{$i};
  $count = int($N /$i);
  for(my $j=2;$j<=$count;$j++){
    $del{$i}= $del{$i} - $del{$i*$j};
 }
 $sum = $sum + $i*$del{$i};
 #print "i$i del$del{$i} $sum\n";
}
$sum = $sum + $del{1};
print "$sum";
exit;


my $N = <STDIN>;
if($N==1){print "$N";exit;}
my $K = 3;
my $sum =0;
my %del;
for(my $i=$N;$i>0;$i--){
 $count = int($N /$i);
 $dell  =1;
 for(my $j=0;$j<$K;$j++){
     $dell = $dell * $count;
 }
 $del{$i} = $dell;
 for(my $j=2;$j<=$count;$j++){
    $del{$i}= $del{$i} - $del{$i*$j};
 }
 $sum = $sum + $i*$del{$i};
}
print "$sum";


my $N = <STDIN>;
if($N==1){print "$N";exit;}
my $K = 3;
my $sum =0;
my %del;
for(my $i=$N;$i>0;$i--){
 $count = int($N /$i);
 $dell  =1;
 for(my $j=0;$j<$K;$j++){
     $dell = $dell * $count;
 }
 $del{$i} = $dell;
 for(my $k=2;$k<=$count;$k++){
    $del{$i}= $del{$i} - $del{$i*$k};
 }
 $sum = $sum + $i*$del{$i};
}
print "$sum";
exit;


my $N = <STDIN>;
my $K = 3;
my $sum =0;
my %del;
for(my $i=$N;$i>0;$i--){
 $count = int($N /$i);
 $dell  =1;
 for(my $j=0;$j<$K;$j++){
     $dell = $dell * $count;
 }
 $del{$i} = $dell;
 for(my $j=2;$j<=$count;$j++){
    $del{$i}= $del{$i} - $del{$i*$j};
 }
 $sum = $sum + $i*$del{$i};
}
print "$sum";
exit;


my $N = <STDIN>;
if($N==1){print "$N";exit;}
my $K = 3;
my $sum =0;
my %del;
for(my $i=$N;$i>0;$i--){
 $count = int($N /$i);
 $dell  =1;
 for(my $j=0;$j<$K;$j++){
     $dell = $dell * $count;
 }
 $del{$i} = $dell;
 for(my $j=2;$j<=$count;$j++){
    $del{$i}= $del{$i} - $del{$i*$j};
 }
 $sum = $sum + $i*$del{$i};
 #print "i$i del$del{$i} $sum\n";
}
print "$sum";
exit;


my $N = <STDIN>;

my $sum =0;
my %del;
for(my $i=$N;$i>0;$i--){
 $count = int($N /$i);
 $del{$i} = ($count*$count*$count);
 #print "i$i count$count del$del{$i}  \n";
}
for(my $i=$N;$i>0;$i--){
 #print "i$i  del$del{$i} \n";
 for(my $j=1;$j<$i;$j++){
    if($j==1){ $del{1}=$del{1} - $del{$i};}
    if( ($i%$j == 0) ){ $del{$j}= $del{$j} - $del{$i};}
 }
 $sum = $sum + $i*$del{$i};
 #print "i$i del$del{$i} $sum\n";
}
print "$sum";
exit;


my $N = <STDIN>;
my $sum =0;
my %del;
for(my $i=$N;$i>0;$i--){
 $count = int($N /$i);
 $del{$i} = ($count*$count*$count);
 #print "i$i count$count del$del{$i}  \n";
}
for(my $i=$N;$i>1;$i--){
 #print "i$i  del$del{$i} \n";
 $del{1}=$del{1} - $del{$i};
 for(my $j=2;$j<$i;$j++){
    if( ($i%$j == 0) ){
        $del{$j}= $del{$j} - $del{$i};
    }
 }
 $sum = $sum + $i*$del{$i};
 #print "i$i del$del{$i} $sum\n";
}
$sum = $sum + $del{1};
print "$sum";
exit;


use v5.26; # strict say state biwise
use warnings;
use List::Util qw(reduce any all none notall first max maxstr min minstr product sum sum0 pairs unpairs pairkeys pairvalues pairfirst pairgrep pairmap shuffle uniq uniqnum uniqstr);

chomp (my $k = <STDIN>);
my @memo;
sub gcd {
  my ($x, $y) = @_;
  return $memo[$x][$y] if $memo[$x][$y] > 0;
  my ($u, $v) = ($x, $y);
  while ($y) {
    my $r = $x % $y;
    $x = $y;
    $y = $r;
  }
  $memo[$u][$v] = $x;
}
for my $i (1..200) {
  for my $j (1..200) {
    gcd($i, $j);
  }
}
my $ans = 0;
for my $i (1..$k) {
  for my $j (1..$k) {
    my $c = $memo[$i][$j];
    for my $l (1..$k) {
      $ans += $memo[$c][$l];
    }
  }
}
say $ans;


use v5.26; # strict say state biwise
use warnings;

chomp (my $k = <STDIN>);
my $ans = 0;
sub gcd {
  my ($x, $y) = @_;
  while ($y) {
    my $r = $x % $y;
    $x = $y;
    $y = $r;
  }
  $x;
}
for my $i (1..$k) {
  for my $j (1..$k) {
    my $c = gcd($i, $j);
    for my $l (1..$k) {
      $ans += gcd($c, $l);
    }
  }
}
say $ans;


use strict;
use warnings;
use v5.18.0;
use Math::BigInt qw(bgcd);
use List::Util qw(sum);

chomp(my $arg = <>);

my @nums;

for (my $a = 1; $a <= $arg; $a++) {
  for (my $b = 1; $b <= $arg; $b++) {
    for (my $c = 1; $c <= $arg; $c++) {
      push(@nums, bgcd($a, $b, $c));
    }
  }
}

print sum(@nums)."\n";


use strict;
use warnings;
use utf8;

sub gcd {
    my ($a, $b) = @_;
    return ($b == 0) ? $a : gcd($b, $a % $b);
}

my $k = <STDIN>;
my $sum = 0;

my @first = ();
for my $i (1..$k) {
    my $second = [];
    push @first, $second;
}

for my $a (1..$k) {
    for my $b (1..$k) {
        my $temp = ($first[$a][$b]) ? $first[$a][$b] : gcd($a, $b);
        $first[$a][$b] = $temp;
        $first[$b][$a] = $temp;
        for my $c (1..$k) {
            my $temp2 = ($first[$temp][$c]) ? $first[$temp][$c] : gcd($temp, $c);
            $first[$temp][$c] = $temp2;
            $first[$c][$temp] = $temp2;
            $sum += $temp2;
        }
    }
}

print $sum;


use v5.26; # strict say state biwise
use warnings;
use List::Util qw(reduce any all none notall first max maxstr min minstr product sum sum0 pairs unpairs pairkeys pairvalues pairfirst pairgrep pairmap shuffle uniq uniqnum uniqstr);

chomp (my $k = <STDIN>);
my $ans = 0;
my @cal;
my $gcd = sub {
  my ($x, $y) = @_;
  return $cal[$x][$y] if $cal[$x][$y]>0;
  my ($v, $u) = ($x, $y);
  while ($y) {
    my $r = $x % $y;
    $r += $y if $r < 0;
    $x = $y;
    $y = $r;
  }
  $cal[$v][$u] = $x;
  return $x;
};
for my $i (1..$k) {
  for my $j (1..$k) {
    my $c = $gcd->($i, $j);
    for my $l (1..$k) {
      $ans += $gcd->($c, $l);
    }
  }
}
say $ans;


use v5.26; # strict say state biwise
use warnings;
use List::Util qw(reduce any all none notall first max maxstr min minstr product sum sum0 pairs unpairs pairkeys pairvalues pairfirst pairgrep pairmap shuffle uniq uniqnum uniqstr);

chomp (my $k = <STDIN>);
my $ans = 0;
my %h;
my $gcd = sub {
  my ($x, $y) = @_;
  return $h{[$x, $y]} if defined $h{[$x, $y]};
  my ($v, $u) = ($x, $y);
  while ($y) {
    my $r = $x % $y;
    $r += $y if $r < 0;
    $x = $y;
    $y = $r;
  }
  $h{[$v, $u]} = $x;
  return $x;
};
for my $i (1..$k) {
  for my $j (1..$k) {
    my $c = $gcd->($i, $j);
    for my $l (1..$k) {
      $ans += $gcd->($c, $l);
    }
  }
}
say $ans;


Score : 400 points

Problem Statement

We have a string S of length N consisting of R, G, and B.

Find the number of triples (i,~j,~k)~(1 \leq i < j < k \leq N) that satisfy both of the following conditions:

S_i \neq S_j, S_i \neq S_k, and S_j \neq S_k.

j - i \neq k - j.

Constraints

1 \leq N \leq 4000

S is a string of length N consisting of R, G, and B.

Input

Input is given from Standard Input in the following format:

N
S

Output

Print the number of triplets in question.

Sample Input 1

4
RRGB

Sample Output 1

1

Only the triplet (1,~3,~4) satisfies both conditions. The triplet (2,~3,~4) satisfies the first condition but not the second, so it does not count.

Sample Input 2

39
RBRBGRBGGBBRRGBBRRRBGGBRBGBRBGBRBBBGBBB

Sample Output 2

1800


use v5.26; # strict say state biwise
use warnings;
use List::Util qw(reduce any all none notall first max maxstr min minstr product sum sum0 pairs unpairs pairkeys pairvalues pairfirst pairgrep pairmap shuffle uniq uniqnum uniqstr);

chomp (my $n = <STDIN>);
chomp (my $s = <STDIN>);
my ($ans, $r, $g, $b) = (0, 0, 0, 0);
for my $i (0..$n-1) {
  if (substr($s, $i, 1) eq 'R') {
    $r++;
  } elsif (substr($s, $i, 1) eq 'G') {
    $g++;
  } else {
    $b++;
  }
  for my $j ($i+1..$n-1) {
    my $k = $j + $j - $i;
    last if $k >= $n;
    $ans++ if substr($s, $i, 1) ne substr($s, $j, 1) && substr($s, $i, 1) ne substr($s, $k, 1) && substr($s, $j, 1) ne substr($s, $k, 1);
  }
}
say ($r * $g * $b - $ans);


use v5.26; # strict say state biwise
use warnings;
use List::Util qw(reduce any all none notall first max maxstr min minstr product sum sum0 pairs unpairs pairkeys pairvalues pairfirst pairgrep pairmap shuffle uniq uniqnum uniqstr);

chomp (my $n = <STDIN>);
chomp (my $s = <STDIN>);
my $ans = 0;
for my $i (0..$n-1) {
  for my $j ($i+1..$n-1) {
    next if substr($s, $i, 1) eq substr($s, $j, 1);
    for my $k ($j+1..$n-1) {
      next if substr($s, $i, 1) eq substr($s, $k, 1) || substr($s, $j, 1) eq substr($s, $k, 1);
      next if $j - $i == $k - $j;
      $ans++;
    }
  }
}
say $ans;


use v5.26; # strict say state biwise
use warnings;
use List::Util qw(reduce any all none notall first max maxstr min minstr product sum sum0 pairs unpairs pairkeys pairvalues pairfirst pairgrep pairmap shuffle uniq uniqnum uniqstr);

chomp (my $n = <STDIN>);
chomp (my $s = <STDIN>);
my @s = (0, split('', $s));
my (@r, @g, @b);
for my $i (1..$n) {
  if ($s[$i] eq 'R') {
    unshift @r, $i;
  } elsif($s[$i] eq 'G') {
    unshift @g, $i;
  } else {
    unshift @b, $i;
  }
}
my $ans = 0;
for my $i (@r) {
  for my $j (@g) {
    last if $i > $j;
    for my $k (@b) {
      last if $j > $k;
      $ans++ if $j - $i != $k - $j;
    }
  }
}
for my $i (@r) {
  for my $j (@b) {
    last if $i > $j;
    for my $k (@g) {
      last if $j > $k;
      $ans++ if $j - $i != $k - $j;
    }
  }
}
for my $i (@g) {
  for my $j (@r) {
    last if $i > $j;
    for my $k (@b) {
      last if $j > $k;
      $ans++ if $j - $i != $k - $j;
    }
  }
}
for my $i (@g) {
  for my $j (@b) {
    last if $i > $j;
    for my $k (@r) {
      last if $j > $k;
      $ans++ if $j - $i != $k - $j;
    }
  }
}
for my $i (@b) {
  for my $j (@r) {
    last if $i > $j;
    for my $k (@g) {
      last if $j > $k;
      $ans++ if $j - $i != $k - $j;
    }
  }
}
for my $i (@b) {
  for my $j (@g) {
    last if $i > $j;
    for my $k (@r) {
      last if $j > $k;
      $ans++ if $j - $i != $k - $j;
    }
  }
}
say $ans;


use strict;
use warnings;
use utf8;

my $n = int(<STDIN>);
my @ss = split '', <STDIN>;

my @r_cnt = ();
my @g_cnt = ();
my @b_cnt = ();

for my $i (0..$#ss) {
    if ($ss[$i] eq 'R') {
        push @r_cnt, $i;
    }
    elsif ($ss[$i] eq 'G') {
        push @g_cnt, $i;
    }
    elsif ($ss[$i] eq 'B') {
        push @b_cnt, $i;
    }
}

my $ans = scalar(@r_cnt) * scalar(@g_cnt) * scalar(@b_cnt);

for my $i (@r_cnt) {
    for my $j (@g_cnt) {
        for my $k (@b_cnt) {
            my ($x, $y, $z) = (($i - $j)**2, ($i - $k)**2, ($j - $k)**2);
            if( $x == $y || $x == $z || $y == $z ) {
                $ans--;
            }
        }
    }
}

print $ans;


use v5.26; # strict say state biwise
use warnings;
use List::Util qw(reduce any all none notall first max maxstr min minstr product sum sum0 pairs unpairs pairkeys pairvalues pairfirst pairgrep pairmap shuffle uniq uniqnum uniqstr);

chomp (my $n = <STDIN>);
chomp (my $s = <STDIN>);
my @s = (0, split('', $s));
my (@r, @g, @b);
for my $i (1..$n) {
  if ($s[$i] eq 'R') {
    push @r, $i;
  } elsif($s[$i] eq 'G') {
    push @g, $i;
  } else {
    push @b, $i;
  }
}
my $ans = 0;
for my $i (@r) {
  for my $j (@g) {
    for my $k (@b) {
      my $max = max($i, $j, $k);
      my $min = min($i, $j, $k);
      my $mid = $i + $j + $k - $max - $min;
      $ans++ if $mid - $min != $max - $mid;
    }
  }
}
say $ans;


use v5.26; # strict say state biwise
use warnings;
use List::Util qw(reduce any all none notall first max maxstr min minstr product sum sum0 pairs unpairs pairkeys pairvalues pairfirst pairgrep pairmap shuffle uniq uniqnum uniqstr);

chomp (my $n = <STDIN>);
chomp (my $s = <STDIN>);
my @s = (0, split('', $s));
my @p;
for my $i (1..$n) {
  push @p, $s[$i];
  push @p, $i;
}
my (@r, @g, @b);
@r = pairvalues pairgrep { $a eq 'R' } @p;
@g = pairvalues pairgrep { $a eq 'G' } @p;
@b = pairvalues pairgrep { $a eq 'B' } @p;
my $ans = 0;
for my $i (@r) {
  for my $j (@g) {
    for my $k (@b) {
      my $max = max($i, $j, $k);
      my $min = min($i, $j, $k);
      my $mid = sum($i, $j, $k) - $max - $min;
      $ans++ if $mid - $min != $max - $mid;
    }
  }
}
say $ans;


use v5.26; # strict say state biwise
use warnings;
use List::Util qw(reduce any all none notall first max maxstr min minstr product sum sum0 pairs unpairs pairkeys pairvalues pairfirst pairgrep pairmap shuffle uniq uniqnum uniqstr);

chomp (my $n = <STDIN>);
chomp (my $s = <STDIN>);
my @s = (0, split('', $s));
my @p;
for my $i (1..$n) {
  push @p, $s[$i];
  push @p, $i;
}
my (@r, @g, @b);
@r = sort { $a <=> $b } pairvalues pairgrep { $a eq 'R' } @p;
@g = sort { $a <=> $b } pairvalues pairgrep { $a eq 'G' } @p;
@b = sort { $a <=> $b } pairvalues pairgrep { $a eq 'B' } @p;
my $ans = 0;
for my $i (@r) {
  for my $j (@g) {
    for my $k (@b) {
      my $max = max($i, $j, $k);
      my $min = min($i, $j, $k);
      my $mid = sum($i, $j, $k) - $max - $min;
      $ans++ if $mid - $min != $max - $mid;
    }
  }
}
say $ans;


use v5.26; # strict say state biwise
use warnings;
use List::Util qw(reduce any all none notall first max maxstr min minstr product sum sum0 pairs unpairs pairkeys pairvalues pairfirst pairgrep pairmap shuffle uniq uniqnum uniqstr);

chomp (my $n = <STDIN>);
chomp (my $s = <STDIN>);
my @s = (0, split('', $s));
my $ans = 0;
for my $i (1..$n) {
  for my $j ($i+1..$n) {
    for my $k ($j+1..$n) {
      if ($s[$i] ne $s[$j] && $s[$i] ne $s[$k] && $s[$j] ne $s[$k] && $j-$i != $k-$j) {
        $ans++;
      }
    }
  }
}
say $ans;


Score : 100 points

Problem Statement

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

Currently, the boxes A, B, and C contain the integers X, Y, and Z, respectively.

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

Swap the contents of the boxes A and B

Swap the contents of the boxes A and C

Constraints

1 \leq X,Y,Z \leq 100

All values in input are integers.

Input

Input is given from Standard Input in the following format:

X Y Z

Output

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

Sample Input 1

1 2 3

Sample Output 1

3 1 2

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

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

Sample Input 2

100 100 100

Sample Output 2

100 100 100

Sample Input 3

41 59 31

Sample Output 3

31 41 59


#!usr/bin/perl

use strict;
my ($a, $b, $c) = split/ /,<>;

print ("$c $a $b");


use strict;
use warnings;

chomp(my $xyz = <>);
my ($x, $y, $z) = split(/ /, $xyz);
print "$z $x $y \n";


use strict;
use warnings;

chomp(my $input = <STDIN>);

my ($a, $b, $c) = split(/ /, $input);
print "$c $a $b \n";


use strict;
use warnings;

chomp(my $xyz = <>);

my ($x, $y, $z) = split(/ /, $xyz);

print "$z $x $y\n";


use v5.18; # strict say state
use warnings;
use List::Util qw(reduce first max min sum0);

chomp (my ($x, $y, $z) = split / /, <STDIN>);
($x, $y) = ($y, $x);
($x, $z) = ($z, $x);
say "$x $y $z";


$s = <STDIN>;

my @dat =  split(/ /,$s);

print "$dat[2] $dat[0] $dat[1]";


Score : 200 points

Problem Statement

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

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

If M popular items can be selected, print Yes; otherwise, print No.

Constraints

1 \leq M \leq N \leq 100

1 \leq A_i \leq 1000

A_i are distinct.

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N M
A_1 ... A_N

Output

If M 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 12 votes in total. The most popular item received 5 votes, and we can select it.

Sample Input 2

3 2
380 19 1

Sample Output 2

No

There were 400 votes in total. The second and third most popular items received less than \dfrac{1}{4\times 2} 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


use strict;
use warnings;
use List::Util qw(sum);

chomp(my $nm = <>);
my ($n, $m) = split(/ /, $nm);

chomp(my $input_a = <>);
my @a = split(/ /, $input_a);
@a = sort {$b <=> $a} @a;

# 総投票数
my $sum = sum(@a);

if ($a[0] >= $sum/(4*$m)) {
  print "Yes\n";
} else {
  print "No\n";
}


use strict;
use warnings;

sub sum {
    my $total = 0;
    foreach my $x (@_) {
        $total += $x;
    }
    return $total;
}

chomp (my ($N, $M)= split(/ /, <STDIN>));
chomp (my @A = split(/ /, <STDIN>));

my $total = sum(@A);
my $count = 0;

foreach my $item (@A) {
    if ($total / (4 * $M) <= $item) {
        $count++;
    }
}

if ($M <= $count) {
    print "Yes\n";
} else {
    print "No\n";
}


use strict;
use warnings;

chomp(my $nm = <>);
my ($n, $m) = split(/ /, $nm);

chomp(my $input_a = <>);
my @a = split(/ /, $input_a);
my $a_ref = \@a;
@$a_ref = sort {$b <=> $a} @$a_ref;

my $sum;
for my $i (0..$n-1) {
  $sum += $a_ref->[$i];
}

if ($a_ref->[$m-1] >= $sum/(4*$m)) {
  print "Yes\n";
} else {
  print "No\n";
}


use List::Util qw(max min sum);

my ($n,$m) = (split(/\s+/, <>));
my (@a) = (split(/\s+/, <>));

my $th = sum(@a)/4/$m;

my $c = 0;
for (@a) {
    $c++ if $_>=$th;
}
if ($c>=$m) {
    print "Yes\n";
} else {
    print "No\n";
}


$s = <STDIN>;
my @dat =  split(/ /,$s);
my $N =  $dat[0] ;
my $M =  $dat[1] ;
$s = <STDIN>;
my @dat =  split(/ /,$s);
my $sum=0;
for($i=0;$i<$N;$i++){
  $sum = $sum + $dat[$i];
}
my $min = $sum /(4*$M);
my @h = sort { $b - $a } @dat;
#print "$min  $sum";
if( $min <= $h[$M-1]){
   print "Yes";
}else{
 print "No";
}


$s = <STDIN>;
my @dat =  split(/ /,$s);
my $N =  $dat[0] ;
my $M =  $dat[1] ;
$s = <STDIN>;
my @dat =  split(/ /,$s);
my $sum=0;
for($i=0;$i<$N;$i++){
  $sum = $sum + $dat[$i];
}
my $min = $sum /(4*$M);
my @h = sort { $b - $a } @dat;
#print "$min  $sum";
if( $min < $h[$M-1]){
   print "Yes";
}else{
 print "No";
}


Score : 500 points

Problem Statement

You are going to eat X red apples and Y green apples.

You have A red apples of deliciousness p_1,p_2, \dots, p_A, B green apples of deliciousness q_1,q_2, \dots, q_B, and C colorless apples of deliciousness r_1,r_2, \dots, r_C.

Before eating a colorless apple, you can paint it red or green, and it will count as a red or green apple, respectively.

From the apples above, you will choose the apples to eat while making the sum of the deliciousness of the eaten apples as large as possible.

Find the maximum possible sum of the deliciousness of the eaten apples that can be achieved when optimally coloring zero or more colorless apples.

Constraints

1 \leq X \leq A \leq 10^5

1 \leq Y \leq B \leq 10^5

1 \leq C \leq 10^5

1 \leq p_i \leq 10^9

1 \leq q_i \leq 10^9

1 \leq r_i \leq 10^9

All values in input are integers.

Input

Input is given from Standard Input in the following format:

X Y A B C
p_1 p_2 ... p_A
q_1 q_2 ... q_B
r_1 r_2 ... r_C

Output

Print the maximum possible sum of the deliciousness of the eaten apples.

Sample Input 1

1 2 2 2 1
2 4
5 1
3

Sample Output 1

12

The maximum possible sum of the deliciousness of the eaten apples can be achieved as follows:

Eat the 2-nd red apple.

Eat the 1-st green apple.

Paint the 1-st colorless apple green and eat it.

Sample Input 2

2 2 2 2 2
8 6
9 1
2 1

Sample Output 2

25

Sample Input 3

2 2 4 4 4
11 12 13 14
21 22 23 24
1 2 3 4

Sample Output 3

74


use strict;
use warnings;

chomp(my $abc = <>);
my ($a, $b, $c) = split(" ", $abc);

if ( 4*$a*$b < ($c-$a-$b)**2 ){
  print "Yes\n";
} else {
  print "No\n";
}

Score : 300 points

Problem Statement

Does \sqrt{a} + \sqrt{b} < \sqrt{c} hold?

Constraints

1 \leq a, b, c \leq 10^9

All values in input are integers.

Input

Input is given from Standard Input in the following format:

a \ b \ c

Output

If \sqrt{a} + \sqrt{b} < \sqrt{c}, print Yes; otherwise, print No.

Sample Input 1

2 3 9

Sample Output 1

No

\sqrt{2} + \sqrt{3} < \sqrt{9} does not hold.

Sample Input 2

2 3 10

Sample Output 2

Yes

\sqrt{2} + \sqrt{3} < \sqrt{10} holds.


use strict;
use warnings;

chomp(my $abc = <>);
my ($a, $b, $c) = split(" ", $abc);

if (sqrt($a*$b) < ($c-$a-$b)/2){
  print "Yes\n";
} else {
  print "No\n";
}


use v5.18; # strict say state
use warnings;
use bignum(a=>14);

my ($a, $b, $c) = split / /, <STDIN>;

my $x = sqrt($a);
my $y = sqrt($b);
my $z = sqrt($c);

if ($x+$y<$z) {
  say "Yes";
} else {
  say "No";
}


use v5.18; # strict say state
use warnings;
use bignum(a=>20);

my ($a, $b, $c) = split / /, <STDIN>;

my $x = sqrt($a);
my $y = sqrt($b);
my $z = sqrt($c);

if ($x+$y<$z) {
  say "Yes";
} else {
  say "No";
}


use v5.18; # strict say state
use warnings;
use bignum(a=>60);

my ($a, $b, $c) = split / /, <STDIN>;

my $x = sqrt($a);
my $y = sqrt($b);
my $z = sqrt($c);

if ($x+$y<$z) {
  say "Yes";
} else {
  say "No";
}


use v5.18; # strict say state
use warnings;
use bignum(a=>60);

my ($a, $b, $c) = split / /, <STDIN>;

my $x = new Math::BigFloat($a)->bsqrt(2);
my $y = new Math::BigFloat($b)->bsqrt(2);
my $z = Math::BigFloat->new($c)->bsqrt(2);

if ($x+$y<$z) {
  say "Yes";
} else {
  say "No";
}


use v5.18; # strict say state
use warnings;
use bignum(a=>40);

my ($a, $b, $c) = split / /, <STDIN>;

my $x = new Math::BigFloat($a)->bsqrt(2);
my $y = new Math::BigFloat($b)->bsqrt(2);
my $z = Math::BigFloat->new($c)->bsqrt(2);

if ($x+$y<$z) {
  say "Yes";
} else {
  say "No";
}


use v5.18; # strict say state
use warnings;
use Math::BigFloat;

my ($a, $b, $c) = split / /, <STDIN>;

my $x = Math::BigFloat->new(sqrt($a));
my $y = Math::BigFloat->new(sqrt($b));
my $z = Math::BigFloat->new(sqrt($c));

if ($x+$y<$z) {
  say "Yes";
} else {
  say "No";
}


use v5.18; # strict say state
use warnings;

my ($a, $b, $c) = split / /, <STDIN>;
if (4*$a*$b<($c-$a-$b)**2 && $c-$a-$b>0) {
  say "Yes";
} else {
  say "No";
}


use v5.18; # strict say state
use warnings;

my ($a, $b, $c) = split / /, <STDIN>;
if (4*$a*$b<($c-$a-$b)**2) {
  say "Yes";
} else {
  say "No";
}


$s = <STDIN>;

my @dat =  split(/ /,$s);

my $A = sqrt($dat[0]);
my $B = sqrt($dat[1]);
my $C = sqrt($dat[2]);

$L = $dat[0] + $dat[1] + (2 * $A * $B);
$M = $dat[2];
if($L< $M){
  print "Yes";
}else{
  print "No";
}


$s = <STDIN>;

my @dat =  split(/ /,$s);

my $A = sqrt($dat[0]);
my $B = sqrt($dat[1]);
my $C = sqrt($dat[2]);

if(($A + $B)< $C){
  print "Yes";
}else{
  print "No";
}


$s = <STDIN>;

my @dat =  split(/ /,$s);

$A = sqrt($dat[0]) + sqrt($dat[1]);
$B = sqrt($dat[2]);

if($A < $B){
  print "Yes";
}else{
  print "No";
}


use v5.18; # strict say state
use warnings;
use List::Util qw(reduce first max min sum0);

my ($a, $b, $c) = split / /, <STDIN>;
if (sqrt($a)+sqrt($b)<sqrt($c)) {
  say "Yes";
} else {
  say "No";
}


Score : 300 points

Problem Statement

If there is an integer not less than 0 satisfying the following conditions, print the smallest such integer; otherwise, print -1.

The integer has exactly N digits in base ten. (We assume 0 to be a 1-digit integer. For other integers, leading zeros are not allowed.)

The s_i-th digit from the left is c_i. \left(i = 1, 2, \cdots, M\right)

Constraints

All values in input are integers.

1 \leq N \leq 3

0 \leq M \leq 5

1 \leq s_i \leq N

0 \leq c_i \leq 9

Input

Input is given from Standard Input in the following format:

N M
s_1 c_1
\vdots
s_M c_M

Output

Print the answer.

Sample Input 1

3 3
1 7
3 2
1 7

Sample Output 1

702

702 satisfies the conditions - its 1-st and 3-rd digits are 7 and 2, respectively - while no non-negative integer less than 702 satisfies them.

Sample Input 2

3 2
2 1
2 3

Sample Output 2

-1

Sample Input 3

3 1
1 0

Sample Output 3

-1


use strict;
use warnings;
my $l = <STDIN>;
chomp($l);
my @line = split(/ /,$l);
my $n = shift @line;
my $m = shift @line;
my @result = (-1,-1,-1);
for (my $i = 0; $i < $m; $i++) {
  my @line = split(/ /,<STDIN>);
  my $s = shift @line;
  my $c = shift @line;
  chomp($c);
  if(($result[$s - 1] != -1 && $result[$s - 1] != $c) || ($s == 1 && $c ==0) ) {
    print -1;
    exit;
  };
  $result[$s - 1] = $c;
}
if($n == 1 && ($result[0] == 0 || $result[0] == -1)) {
  print 0;
  exit;
}
for (my $i = 0; $i < $n; $i++) {
  my $val = $result[$i];
  if($val < 0) {
    if($i == 0){
  	  $val = 1;
    } else {
  	  $val = 0;
    }
  }
  print $val;
}


use strict;
use warnings;
my $l = <STDIN>;
chomp($l);
my @line = split(/ /,$l);
my $n = shift @line;
my $m = shift @line;
my @result = (-1,-1,-1);
for (my $i = 0; $i < $m; $i++) {
  my @line = split(/ /,<STDIN>);
  my $s = shift @line;
  my $c = shift @line;
  chomp($c);
  if(($result[$s - 1] != -1 && $result[$s - 1] != $c) || ($s == 1 && $c ==0) ) {
    print -1;
    exit;
  };
  $result[$s - 1] = $c;
}
for (my $i = 0; $i < $n; $i++) {
  my $val = $result[$i];
  if($val < 0) {
    if($i == 0){
  	  $val = 1;
    } else {
  	  $val = 0;
    }
  }
  print $val;
}


use strict;
use warnings;
my $l = <STDIN>;
chomp($l);
my @line = split(/ /,$l);
my $n = shift @line;
my $m = shift @line;
my @result = (-1,-1,-1);
for (my $i = 0; $i < $m; $i++) {
  my @line = split(/ /,<STDIN>);
  my $s = shift @line;
  my $c = shift @line;
  chomp($c);
  if($result[$s - 1] != -1 && $result[$s - 1] != $c || ($s == 1 && $c ==0) ) {
    print -1;
    exit;
  };
  $result[$s - 1] = $c;
}
for (my $i = 0; $i < $n; $i++) {
  my $val = $result[$i];
  if($val < 0) {
    if($i == 0){
  	$val = 1;
    } else {
  	$val = 0;
    }
  }
  print $val;
}


use strict;
use warnings;
my $l = <STDIN>;
chomp($l);
my @line = split(/ /,$l);
my $n = shift @line;
my $m = shift @line;
my @result = (-1,-1,-1);
for (my $i = 0; $i < $m; $i++) {
  my @line = split(/ /,<STDIN>);
  my $s = shift @line;
  my $c = shift @line;
  chomp($c);
  if($result[$s - 1] != -1 && $result[$s - 1] != $c || ($s == 1 && $c ==0) ) {
    print -1;
    exit;
  };
  $result[$s - 1] = $c;
}
for (my $i = 0; $i < $n; $i++) {
  my $val = $result[$i];
  if($val < 0) {
  	$val = 1;
  }
  print $val;
}


use strict;
use warnings;
my $l = <STDIN>;
chomp($l);
my @line = split(/ /,$l);
my $n = shift @line;
my $m = shift @line;
my @result = (-1,-1,-1);
for (my $i = 0; $i < $m; $i++) {
  my @line = split(/ /,<STDIN>);
  my $s = shift @line;
  my $c = shift @line;
  chomp($c);
  if($result[$s - 1] != -1 && $result[$s - 1] != $c || ($s == 0 && $c ==0) ) {
    print -1;
    exit;
  };
  $result[$s - 1] = $c;
}
if($result[0] == 0 || $result[0] == -1) {
    print -1;
    exit;
}
for (my $i = 0; $i < $n; $i++) {
  my $val = $result[$i];
  if($val < 0) {
  	$val = 1;
  }
  print $val;
}


use strict;
use warnings;
my $l = <STDIN>;
chomp($l);
my @line = split(/ /,$l);
my $n = shift @line;
my $m = shift @line;
my @result = (-1,-1,-1);
for (my $i = 0; $i < $m; $i++) {
  my @line = split(/ /,<STDIN>);
  my $s = shift @line;
  my $c = shift @line;
  chomp($c);
  if($result[$s - 1] != -1 && $result[$s - 1] != $c) {
    print -1;
    exit;
  };
  $result[$s - 1] = $c;
}
if($result[0] == 0 || $result[0] == -1) {
    print -1;
    exit;
}
for (my $i = 0; $i < $n; $i++) {
  my $val = $result[$i];
  if($val < 0) {
  	$val = 0;
  }
  print $val;
}


use strict;
use warnings;
my $line = <STDIN>;
chomp($line);
my @line = split(/ /,$line);
my $n = shift @line;
my $m = shift @line;
my @result = (-1,-1,-1);
for (my $i = 0; $i < $m; $i++) {
  my @line = split(/ /,<STDIN>);
  my $s = shift @line;
  my $c = shift @line;
  chomp($c);
  if(@result[$s - 1] != -1 && @result[$s - 1] != $c) {
    print -1;
    exit;
  };
  @result[$s - 1] = $c;
}
if(@result[0] == 0 || @result[0] == -1) {
    print -1;
    exit;
}
foreach my $val (@result) {
  if($val < 0) {
  	$val = 0;
  }
  print $val;
}


use strict;
use warnings;

chomp(my $nm = <>);
my ($n, $m) = split(/ /, $nm);

my @num;
for my $i (0..$m-1){
  chomp(my $sc = <>);
  (my $s, my $c) = split(/ /, $sc);
  if(defined $num[$s-1] && $num[$s-1] != $c){
    print "-1\n";
    exit;
  }else{
    $num[$s-1] = $c;
  }
}

if ($n > 1){
  if ($num[0] == 0){
    print "-1\n";
    exit;
  }
  if( !(defined $num[0]) ){$num[0]=1;}
}else{
  if( !(defined $num[0]) ){$num[0]=0;}
}

if($m > 1){
  for my $i (1..$m-1){
    if (!(defined $num[$i])){$num[$i]=0;}
  }
}

print @num, "\n";


use strict;
use warnings;

chomp(my $nm = <>);
my ($n, $m) = split(/ /, $nm);

my @num;
for my $i (0..$m-1){
  chomp(my $sc = <>);
  (my $s, my $c) = split(/ /, $sc);
  if(defined $num[$s-1] && $num[$s-1] != $c){
    print "-1\n";
    exit;
  }else{
    $num[$s-1] = $c;
  }
}

if ($num[0] == 0){
  print "-1\n";
  exit;
}

if( !(defined $num[0]) ){$num[0]=1;}

if($m > 1){
  for my $i (1..$m-1){
    if (!(defined $num[$i])){$num[$i]=0;}
  }
}

print @num, "\n";


use strict;
use warnings;

chomp(my $nm = <>);
my ($n, $m) = split(/ /, $nm);

my @num;
for my $i (0..$m-1){
  chomp(my $sc = <>);
  (my $s, my $c) = split(/ /, $sc);
  if(defined $num[$s-1] && $num[$s-1] != $c){
    print "-1\n";
    exit;
  }else{
    $num[$s-1] = $c;
  }
}

if ($num[0] == 0){
  print "-1\n";
  exit;
}

if( !(defined $num[0]) ){$num[0]=1;}

for my $i (1..$m-1){
  if (!(defined $num[$i])){$num[$i]=0;}
}

print @num, "\n";


main();

sub main {
    my ($n, $k) = split ' ', <STDIN>;
    my @a = split ' ', <STDIN>;
    my @mul;
    for (my $i = 1; $i < $n; $i++) {
        for (my $j = 0; $j < $i; $j++) {
            push @mul, $a[$i] * $a[$j];
        }
    }
    @mul = sort {$a <=> $b} @mul;
print @mul;
    print $mul[$k - 1] . "\n";
}

Score: 400 points

Problem Statement

We have N integers A_1, A_2, ..., A_N.

There are \frac{N(N-1)}{2} ways to choose two of them and form a pair. If we compute the product of each of those pairs and sort the results in ascending order, what will be the K-th number in that list?

Constraints

All values in input are integers.

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

1 \leq K \leq \frac{N(N-1)}{2}

-10^9 \leq A_i \leq 10^9\ (1 \leq i \leq N)

Input

Input is given from Standard Input in the following format:

N K
A_1 A_2 \dots A_N

Output

Print the answer.

Sample Input 1

4 3
3 3 -4 -2

Sample Output 1

-6

There are six ways to form a pair. The products of those pairs are 9, -12, -6, -12, -6, 8.

Sorting those numbers in ascending order, we have -12, -12, -6, -6, 8, 9. The third number in this list is -6.

Sample Input 2

10 40
5 4 3 2 -1 0 0 0 0 0

Sample Output 2

6

Sample Input 3

30 413
-170202098 -268409015 537203564 983211703 21608710 -443999067 -937727165 -97596546 -372334013 398994917 -972141167 798607104 -949068442 -959948616 37909651 0 886627544 -20098238 0 -948955241 0 -214720580 277222296 -18897162 834475626 0 -425610555 110117526 663621752 0

Sample Output 3

448283280358331064


main();

sub main {
    my ($n, $k) = split ' ', <STDIN>;
    my @a = split ' ', <STDIN>;
    my @mul;
    for (my $i = 1; $i < $n; $i++) {
        for (my $j = 0; $j < $i; $j++) {
            push @mul, $i * $j;
        }
    }
    @mul = sort {$a <=> $b} @mul;
    print $mul[$k - 1] . "\n";
}


my @nk = glob<>;
my $n = $nk[0];
my $k = $nk[1];
my @a = glob<>;

my @l;
my @removed = [];
for($index=0; $index < @a;$index++){
    $val = $a[$index];
    for($inner_index=@removed; $inner_index < @a;$inner_index++){
        push(@l, $val * $a[$inner_index]);
    }
    push(@removed, $index);
}
@l = sort {$a <=> $b} @l;
print ($l[$k-1]);


my @nk = glob<>;
my $n = $nk[0];
my $k = $nk[1];
my @a = glob<>;

my @l = [];
my @removed = [];
for($index=0; $index < @a;$index++){
    $val = $a[$index];
    for($inner_index=@removed; $inner_index < @a;$inner_index++){
        push(@l, $val * $a[$inner_index]);
    }
    push(@removed, $index);
}
@l = sort {$a <=> $b} @l;
print ($l[$k-1]);


my ($n, $k) = glob<>;
my @a = glob<>;

my @l = [];
foreach my $i (@a) {
    if (@l == 0) {
        push(@l, $i * $j);
        next;
    }

    foreach my $j (@l) {
        push(@l, $i * $j);
    }
}

@l = sort {$a <=> $b} @l;
print ($l[$k]);


use strict;
use warnings;

# N K 入力
chomp(my $nk = <>);
my ($n, $k) = split(/ /, $nk);

# A の入力
chomp(my $aaa = <>);
my @a = split(/ /, $aaa);

# A の分類
my @pos_a;
my @neg_a;
my @zero_a;

for (@a){
  if($_>0){
    push @pos_a, $_;
  } elsif ($_<0) {
    push @neg_a, $_;
  } else {
    push @zero_a, $_;
  }
}

# 積の個数
my $pos_a = @pos_a;
my $neg_a = @neg_a;
my $zero_a = @zero_a;

my $pos = $pos_a * ($pos_a-1)/2 + $neg_a * ($neg_a - 1)/2;
my $neg = $pos_a * $neg_a;
my $zero = @zero_a * ($pos_a + $neg_a) + $zero_a * ($zero_a - 1)/2;

# 正、負、0 に分けて積を算出
if ($k <= $neg) {
  my @neg_no;
  for (my $i = 0; $i < $pos_a; $i++){
    for (my $j = 0; $j < $neg_a; $j++){
      push @neg_no, @pos_a[$i] * @neg_a[$j];
    }
  }
  @neg_no = sort {$a <=> $b} @neg_no;
  print @neg_no[$k - 1] . "\n";
} elsif ($k <= $neg+$zero) {
  print "0\n";
} else {
  my @pos_no;
  for (my $i = 0; $i < $pos_a - 1; $i++){
    for (my $j = $i + 1; $j < $pos_a; $j++){
      push @pos_no, @pos_a[$i] * @pos_a[$j];
    }
  }
  for (my $i = 0; $i < $neg_a - 1; $i++){
    for (my $j = $i + 1; $j < $neg_a; $j++){
      push @pos_no, @neg_a[$i] * @neg_a[$j];
    }
  }
  @pos_no = sort {$a <=> $b} @pos_no;
  print @pos_no[$k -1 - $neg - $zero] . "\n";
}


Score : 100 points

Problem Statement

Having learned the multiplication table, Takahashi can multiply two integers between 1 and 9 (inclusive) together. He cannot do any other calculation.

Given are two integers A and B.

If Takahashi can calculate A \times B, print the result; if he cannot, print -1 instead.

Constraints

1 \leq A \leq 20

1 \leq B \leq 20

All values in input are integers.

Input

Input is given from Standard Input in the following format:

A B

Output

If Takahashi can calculate A \times B, print the result; if he cannot, print -1.

Sample Input 1

2 5

Sample Output 1

10

2 \times 5 = 10.

Sample Input 2

5 10

Sample Output 2

-1

5\times 10 = 50, but Takahashi cannot do this calculation, so print -1 instead.

Sample Input 3

9 9

Sample Output 3

81


use warnings;

$A = <STDIN>;
$B = <STDIN>;

chomp($A,$B);

if ($A >= 1 and $A <= 9 and $B >= 1 and $B <= 9){
  $C = $A * $B;
  print "$C\n";
}
else{
  $C = -1;
  print "$C\n";
}


#!/usr/bin/perl

use strict;
use warnings;

my ($a, $b);
{
    my $in = <STDIN>;
    ($a, $b) = split(" ", $in);
}
my $ans = $a * $b;

print "$ans\n" if $a<10 && $b<10;
print "-1\n" unless $a<10 && $b<10;


#!/usr/bin/perl

use strict;
use warnings;

my ($a, $b);
{
    my $in = <STDIN>;
    ($a, $b) = split(" ", $in);
}
my $ans = $a * $b;

print "$ans\n" if $a<10 && $b<10;
print "-1\n";


Score : 300 points

Problem Statement

Given is a string S. Let T be the concatenation of K copies of S.
We can repeatedly perform the following operation: choose a character in T and replace it with a different character.
Find the minimum number of operations required to satisfy the following condition: any two adjacent characters in T are different.

Constraints

1 \leq |S| \leq 100

S consists of lowercase English letters.

1 \leq K \leq 10^9

K is an integer.

Input

Input is given from Standard Input in the following format:

S
K

Output

Print the minimum number of operations required.

Sample Input 1

issii
2

Sample Output 1

4

T is issiiissii. For example, we can rewrite it into ispiqisyhi, and now any two adjacent characters are different.

Sample Input 2

qq
81

Sample Output 2

81

Sample Input 3

cooooooooonteeeeeeeeeest
999993333

Sample Output 3

8999939997


my @A;
my %h;
foreach my $w (split(//o, <STDIN>)){
    chomp($w);
    next if ($w eq '');
    push(@A, $w);
    $h{$w} = 1;
}
my $N = <STDIN>;
chomp($N);

my $numA = $#A;
my $numK = (keys %h);
if ($numK == 1) {
  my $numA2 = $numA+1;
  if ( ($numA2*$N) % 2 ){
    # odd
    my $tmp = $numA2*$N - 1;
    print $tmp/2;
  } else {
    # even
    my $tmp = $numA2*$N;
    print $tmp/2;
  }
  exit 0;
}
my $oddflag = 0;
my $s = $A[0];
my $e = $A[$numA];
if ( $s eq $e ) {
    my $numS = 0;
    foreach my $w (@A){
        if ( $s eq $w ) {
          $numS++;
        } else {
          last;
        }
    }
    foreach my $w (reverse @A){
        if ( $s eq $w ) {
          $numS++;
        } else {
          last;
        }
    }
    if ( $numS % 2) {
      $oddflag = 1;
    }
}
my $i = 0;
my $b = '';
my $num = 0;
my $lastflag = 0;
foreach my $w (@A){
    if ( $i == 0 ) {
      $s = $w;
      $b = $w;
    } else {
      if ( $b eq $w ) {
        $num++;
        $b = '';
      } elsif ( $i == $numA and $s eq $w and $oddflag ) {
      } elsif ( $i == $numA and $s eq $w ) {
        $num++;
        $lastflag = 1;
      } else {
        $b = $w;
      }
    }
    $i++
}
if ( $i == 1 ) {
  print $N-1;
} else {
  my $v = $num*$N;
  $v-- if ($lastflag);
  print $v;
}


sub main {
    chomp(my $S = <STDIN>);
    chomp(my $K = <STDIN>);
    print solver($S,$K);
}
main() unless caller;

sub solver {
    my ($S,$K) = @_;
    my @ar = split //, $S;
    my $fst = $ar[0];
    my $end = $ar[-1];
    my $idx = 0;
    my @ans;
    my $anser = 0;
    my $len = scalar @ar;
    if($len == 1){
        return 0 if $K == 1;
        return int($K/2);
    }
    if($S =~ /^(.)\1*$/){
        if($len%2 != 0 && $K > 1){
            return int($len * $K /2);
        }
        $anser = int($len/2);
        return $anser * $K;
    }
    for my $i(0 .. $len-1) {
        if(!defined $ans[$idx]) {
            $ans[$idx] = 1;
        }
        if($i < $len-1 && $ar[$i] eq $ar[$i+1]){
            $ans[$idx]++;
            next;
        } elsif($len > $i+1) {
            $idx++;
        }
    }
    for my $n(@ans) {
        next if $n == 1;
        $anser += int($n/2);
    }
    $anser *= $K;
    if($fst eq $end){
        if($ans[0] == 1 && $ans[-1]%2 != 0) {
            $anser += $K-1;
        }
        elsif($ans[-1] == 1 && $ans[0]%2 != 0) {
            $anser += $K-1;
        }
        elsif($ans[0]%2!=0 && $ans[-1]%2!=0 ){
            $anser += $K-1;
        }
    }
    return $anser;
}


sub main {
    chomp(my $S = <STDIN>);
    chomp(my $K = <STDIN>);
    print solver($S,$K);
}
main() unless caller;

sub solver {
    my ($S,$K) = @_;
    my @ar = split //, $S;
    my $fst = $ar[0];
    my $end = $ar[-1];
    my $idx = 0;
    my @ans;
    my $anser = 0;
    my $len = scalar @ar;
    if($len == 1){
        return 0 if $K == 1;
        return int($K/2);
    }
    if($S =~ /^(.)\1*$/){
        if($len%2 != 0 && $K > 1){
            return int($len * $K /2);
        }
        $anser = int($len/2);
        return $anser * $K;
    }
    for my $i(0 .. $len-1) {
        if(!defined $ans[$idx]) {
            $ans[$idx] = 1;
        }
        if($i < $len-1 && $ar[$i] eq $ar[$i+1]){
            $ans[$idx]++;
            next;
        } elsif($len > $i+1) {
            $idx++;
        }
    }
    for my $n(@ans) {
        next if $n == 1;
        $anser += int($n/2);
    }
    $anser *= $K;
    if($fst eq $end){
        if($ans[0] == 1 || $ans[-1] == 1) {
            $anser += $K-1;
        }
        elsif($ans[0]%2!=0 && $ans[-1]%2!=0 ){
            $anser += $K-1;
        }
    }
    return $anser;
}


sub main {
    chomp(my $S = <STDIN>);
    chomp(my $K = <STDIN>);
    print solver($S,$K);
}
main() unless caller;

sub solver {
    my ($S,$K) = @_;
    my @ar = split //, $S;
    my $fst = $ar[0];
    my $end = $ar[-1];
    my $idx = 0;
    my @ans;
    my $anser = 0;
    my $len = scalar @ar;
    if($len == 1){
        return 0 if $K == 1;
        return int($K/2);
    }
    if($S =~ /^(.)\1*$/){
        if($len%2 != 0 && $K > 1){
            return int($len * $K /2);
        }
        $anser = int($len/2);
        return $anser * $K;
    }
    for my $i(0 .. $len-1) {
        if(!defined $ans[$idx]) {
            $ans[$idx] = 1;
        }
        if($i < $len-1 && $ar[$i] eq $ar[$i+1]){
            $ans[$idx]++;
            next;
        } elsif($len > $i+1) {
            $idx++;
        }
    }
    for my $n(@ans) {
        next if $n == 1;
        $anser += int($n/2);
    }
    $anser *= $K;
    if($fst eq $end && ($ans[0] == 1 || $ans[-1] == 1)){
        $anser += $K-1;
    }
    return $anser;
}


sub main {
    chomp(my $S = <STDIN>);
    chomp(my $K = <STDIN>);
    print solver($S,$K);
}
main() unless caller;

sub solver {
    my ($S,$K) = @_;
    my @ar = split //, $S;
    my $fst = $ar[0];
    my $end = $ar[-1];
    my $idx = 0;
    my @ans;
    my $anser = 0;
    my $len = scalar @ar;
    if($len == 1){
        return 0 if $K == 1;
        return int($K/2);
    }
    if($S =~ /^(.)\1*$/){
        if($len%2 != 0 && $K > 1){
            return int($len * $K /2);
        }
        $anser = int($len/2);
        return $anser * $K;
    }
    for my $i(0 .. $len-1) {
        if(!defined $ans[$idx]) {
            $ans[$idx] = 1;
        }
        if($i < $len-1 && $ar[$i] eq $ar[$i+1]){
            $ans[$idx]++;
            next;
        } elsif($len > $i+1) {
            $idx++;
        }
    }
    for my $n(@ans) {
        next if $n == 1;
        $anser += int($n/2);
    }
    $anser *= $K;
    if($fst eq $end && $ans[0] == 1 && $ans[-1] == 1){
        $anser += $K-1;
    }
    return $anser;
}


chomp($s = <>);
chomp($n = <>);

$cnt = 0;
$t = "";

for (split(//, $s)){
	if($t ne ""){
		if($_ eq substr($t,0,1)){
			$t .= $_;
		}else{
			$cnt += int(length($t) / 2);
			$t = $_;
		}
	}else{
		$t = $_;
	}
}

if($cnt == 0){
	$cnt = int(length($t) * $n / 2);
}else{
	$cnt += int(length($t) / 2);
	$cnt = $cnt * $n;
	$cnt = $cnt + $n - 1 if((substr($s, length($s) - 2, 1) ne substr($s, length($s) - 1, 1)) and (substr($s, 0, 1) eq substr($s, length($s) - 1, 1)));
}

print "$cnt\n";


sub main {
    chomp(my $S = <STDIN>);
    chomp(my $K = <STDIN>);
    print solver($S,$K);
}
main() unless caller;

sub solver {
    my ($S,$K) = @_;
    my @ar = split //, $S;
    my $fst = $ar[0];
    my $end = $ar[-1];
    my $idx = 0;
    my @ans;
    my $anser = 0;
    my $len = scalar @ar;
    if($len == 1){
        return 0 if $K == 1;
        return int($K/2);
    }
    if($S =~ /^(.)\1*$/){
        if($len%2 != 0 && $K > 1){
            return int($len * $K /2);
        }
        $anser = int($len/2);
        return $anser * $K;
    }
    for my $i(0 .. $len-1) {
        if(!defined $ans[$idx]) {
            $ans[$idx] = 1;
        }
        if($i < $len-1 && $ar[$i] eq $ar[$i+1]){
            $ans[$idx]++;
            next;
        } elsif($len > $i+1) {
            $idx++;
        }
    }
    my $tmp;
    if($fst eq $end){
        $tmp = $ans[-1];
        $ans[-1] += $ans[0];
        $ans[0] = 1;
    }
    warn(join ":",@ans);
    for my $n(@ans) {
        next if $n == 1;
        $anser += int($n/2);
    }
    $anser *= $K;
    if($fst eq $end){
        $anser -= int($ans[-1]/2);
        $anser += int($tmp/2);
    }
    return $anser;
}


chomp($s = <>);
chomp($n = <>);

$cnt = 0;
$t = "";

for (split(//, $s)){
	if($t ne ""){
		if($_ eq substr($t,0,1)){
			$t .= $_;
		}else{
			$cnt += int(length($t) / 2);
			$t = $_;
		}
	}else{
		$t = $_;
	}
}

if($cnt == 0){
	if(length($s) == 1){
		$cnt = $n;
	}else{
		$cnt += int(length($t) / 2);
		$cnt = $cnt * $n;
	}
	print "$cnt\n";
	exit;
}

$cnt += int(length($t) / 2);
$cnt = $cnt * $n;

$cnt = $cnt + $n - 1 if((substr($s, length($s) - 2, 1) ne substr($s, length($s) - 1, 1)) and (substr($s, 0, 1) eq substr($s, length($s) - 1, 1)));

print "$cnt\n";


sub main {
    chomp(my $S = <STDIN>);
    chomp(my $K = <STDIN>);
    print solver($S,$K);
}
main() unless caller;

sub solver {
    my ($S,$K) = @_;
    my @ar = split //, $S;
    my $fst = $ar[0];
    my $end = $ar[-1];
    my $idx = 0;
    my @ans;
    my $anser = 0;
    my $len = scalar @ar;
    if($len == 1){
        return 0 if $K == 1;
        return int($K/2);
    }
    if($S =~ /^(.)\1*$/){
        if($len%2 != 0 && $K > 1){
            return int($len * $K /2);
        }
        $anser = int($len/2);
        return $anser * $K;
    }
    for my $i(0 .. $len-1) {
        if(!defined $ans[$idx]) {
            $ans[$idx] = 1;
        }
        if($i < $len-1 && $ar[$i] eq $ar[$i+1]){
            $ans[$idx]++;
            next;
        } elsif($len > $i+1) {
            $idx++;
        }
    }
    if($fst eq $end){
        $ans[-1] += $ans[0];
        $ans[0] = 1;
    }
    warn(join ":",@ans);
    for my $n(@ans) {
        next if $n == 1;
        $anser += int($n/2);
    }
    $anser *= $K;
    if($fst eq $end && $K%2!=0){
        $anser--;
    }
    return $anser;
}


my @A;
my %h;
foreach my $w (split(//o, <STDIN>)){
    chomp($w);
    next if ($w eq '');
    push(@A, $w);
    $h{$w} = 1;
}
my $N = <STDIN>;
chomp($N);

my $numA = $#A;
my $numK = (keys %h);
if ($numK == 1 and $numA > 0) {
  my $numA2 = $numA+1;
  if ( ($numA2*$N) % 2 ){
    # odd
    my $tmp = $numA2*$N - 1;
    print $tmp/2;
  } else {
    # even
    my $tmp = $numA2*$N;
    print $tmp/2;
  }
  exit 0;
}
my $oddflag = 0;
my $s = $A[0];
my $e = $A[$numA];
if ( $s eq $e ) {
    my $numS = 0;
    foreach my $w (@A){
        if ( $s eq $w ) {
          $numS++;
        } else {
          last;
        }
    }
    foreach my $w (reverse @A){
        if ( $s eq $w ) {
          $numS++;
        } else {
          last;
        }
    }
    if ( $numS % 2) {
      $oddflag = 1;
    }
}
my $i = 0;
my $b = '';
my $num = 0;
my $lastflag = 0;
foreach my $w (@A){
    if ( $i == 0 ) {
      $s = $w;
      $b = $w;
    } else {
      if ( $b eq $w ) {
        $num++;
        $b = '';
      } elsif ( $i == $numA and $s eq $w and $oddflag ) {
      } elsif ( $i == $numA and $s eq $w ) {
        $num++;
        $lastflag = 1;
      } else {
        $b = $w;
      }
    }
    $i++
}
if ( $i == 1 ) {
  print $N-1;
} else {
  my $v = $num*$N;
  $v-- if ($lastflag);
  print $v;
}