# CCC Junior Problems - Solutions for Beginning Programmers
# These solutions use only basic concepts: variables, if statements, and loops
# No functions, lists, or dictionaries needed!
# Problems ordered by difficulty from easiest to hardest

# ============================================================================
# PROBLEM 3: CCC '18 J2 - Occupy Parking
# Goal: Count how many parking spaces had cars both yesterday AND today
# 'C' means a car was there, '.' means the space was empty
# ============================================================================

# Step 1: Read the number of parking spaces
n = int(input())

# Step 2: Read the parking status for both days
# Each is a string of characters like "CC..C"
yesterday = input()  # What yesterday looked like
today = input()      # What today looks like

# Step 3: Count spaces that had cars both days
count = 0  # Start counting at 0
i = 0      # i will track which parking space we're checking (0 is first space)

# Loop through each parking space
while i < n:  # Keep going while i is less than n
    # Check if space i has a car both days
    # yesterday[i] gets the character at position i in yesterday's string
    # today[i] gets the character at position i in today's string
    if yesterday[i] == 'C' and today[i] == 'C':
        # Both days have 'C' at this position - increment our count
        count = count + 1
    
    # Move to the next parking space
    i = i + 1

# Step 4: Output the result
print(count)


# ============================================================================
# PROBLEM 4: CCC '21 J2 - Silent Auction
# Goal: Find who placed the highest bid
# If there's a tie, the first person to bid that amount wins
# ============================================================================

# Step 1: Read how many people placed bids
n = int(input())

# Step 2: Set up variables to track the winner
highest_bid = 0      # The highest bid amount we've seen so far
winner_name = ""     # The name of the person with the highest bid

# Step 3: Process each bid one by one
i = 0  # Counter for which bid we're processing
while i < n:
    # Read this person's name and bid amount
    name = input()
    bid = int(input())
    
    # Check if this bid is higher than our current highest
    # Note: We use > not >= so ties go to the first bidder
    if bid > highest_bid:
        # This is our new highest bid!
        highest_bid = bid
        winner_name = name
    
    # Move to the next bid
    i = i + 1

# Step 4: Output the winner
print(winner_name)


# ============================================================================
# PROBLEM 5: CCC '15 J2 - Happy or Sad
# Goal: Count emoticons in a message to determine the mood
# :-) is happy face, :-( is sad face
# ============================================================================

# Step 1: Read the message
message = input()

# Step 2: Set up counters for emoticons
happy_count = 0  # How many :-) we find
sad_count = 0    # How many :-( we find

# Step 3: Check every position where a 3-character emoticon could start
i = 0  # Position in the string we're checking
# We stop at len(message) - 3 because we need 3 characters for an emoticon
# For example, if message is "hello", len is 5, so we check positions 0,1,2
while i <= len(message) - 3:
    # Get 3 characters starting at position i
    # message[i:i+3] means "get characters from position i to i+3"
    three_chars = message[i:i+3]
    
    # Check if these 3 characters form an emoticon
    if three_chars == ':-)':
        happy_count = happy_count + 1
    elif three_chars == ':-(': 
        sad_count = sad_count + 1
    
    # Move to the next position
    i = i + 1

# Step 4: Determine the overall mood based on counts
if happy_count == 0 and sad_count == 0:
    # No emoticons found
    print("none")
elif happy_count == sad_count:
    # Equal number of happy and sad
    print("unsure")
elif happy_count > sad_count:
    # More happy than sad
    print("happy")
else:
    # More sad than happy
    print("sad")


# ============================================================================
# PROBLEM 6: CCC '07 J2 - I Speak TXTMSG
# Goal: Translate text message abbreviations to full phrases
# Keep translating until user enters "TTYL"
# ============================================================================

# Keep looping forever (we'll break out when we see TTYL)
while True:
    # Read a text message abbreviation
    short_form = input()
    
    # Translate it using a series of if statements
    # Each if checks for one specific abbreviation
    if short_form == 'CU':
        print("see you")
    elif short_form == ':-)':
        print("I'm happy")
    elif short_form == ':-(': 
        print("I'm unhappy")
    elif short_form == ';-)':
        print("wink")
    elif short_form == ':-P':
        print("stick out my tongue")
    elif short_form == '(~.~)':
        print("sleepy")
    elif short_form == 'TA':
        print("totally awesome")
    elif short_form == 'CCC':
        print("Canadian Computing Competition")
    elif short_form == 'CUZ':
        print("because")
    elif short_form == 'TY':
        print("thank-you")
    elif short_form == 'YW':
        print("you're welcome")
    elif short_form == 'TTYL':
        print("talk to you later")
    else:
        # Not in our translation table - output as-is
        print(short_form)
    
    # Check if we should stop the program
    if short_form == 'TTYL':
        break  # Exit the while loop


# ============================================================================
# PROBLEM 7: CCC '02 J2 - AmeriCanadian
# Goal: Convert American spelling to Canadian spelling
# Rules: If word has 4+ letters, ends in "or", and letter before "or" 
#        is a consonant, change "or" to "our"
# ============================================================================

while True:
    # Read a word
    word = input()
    
    # Check if we should quit
    if word == 'quit!':
        break  # Exit the program
    
    # Check if word should be converted
    # First condition: word must have more than 4 letters
    if len(word) > 4:
        # Second condition: word must end with 'or'
        # word[-2] is the second-last character
        # word[-1] is the last character
        if len(word) >= 2 and word[-2] == 'o' and word[-1] == 'r':
            # Third condition: letter before 'or' must be a consonant
            # word[-3] is the third-last character
            letter_before = word[-3]
            
            # Check if it's a vowel (a,e,i,o,u,y)
            # We'll use a variable to track if we found a vowel
            is_vowel = False
            if letter_before == 'a' or letter_before == 'e' or letter_before == 'i':
                is_vowel = True
            elif letter_before == 'o' or letter_before == 'u' or letter_before == 'y':
                is_vowel = True
            
            # If it's NOT a vowel (i.e., it's a consonant), convert it
            if not is_vowel:
                # Remove last 2 characters and add 'our'
                # word[:-2] means "all characters except the last 2"
                canadian = word[:-2] + 'our'
                print(canadian)
            else:
                # It's a vowel, so don't convert
                print(word)
        else:
            # Doesn't end with 'or'
            print(word)
    else:
        # Word has 4 or fewer letters
        print(word)


# ============================================================================
# PROBLEM 8: CCC '09 J2 - Old Fishin' Hole
# Goal: Find all valid ways to catch fish within point limit
# Each fish type has a point value, total must be <= allowed
# Must catch at least one fish
# ============================================================================

# Step 1: Read the point values
trout_points = int(input())    # Points per brown trout
pike_points = int(input())     # Points per northern pike  
pickerel_points = int(input()) # Points per yellow pickerel
total_allowed = int(input())   # Maximum total points allowed

# Step 2: Set up variables to track results
valid_count = 0   # How many valid combinations we find
output_text = ""  # Build up all output lines as one big string

# Step 3: Try all possible combinations using three nested loops
# Outer loop: number of trout
trout = 0
while trout * trout_points <= total_allowed:  # Stop if trout alone exceed limit
    
    # Middle loop: number of pike
    pike = 0
    # Stop if trout + pike would exceed limit
    while pike * pike_points <= total_allowed - trout * trout_points:
        
        # Inner loop: number of pickerel
        pickerel = 0
        # Stop if all three together would exceed limit
        while pickerel * pickerel_points <= total_allowed - trout * trout_points - pike * pike_points:
            
            # Calculate total points for this combination
            total = trout * trout_points + pike * pike_points + pickerel * pickerel_points
            
            # Check two conditions:
            # 1. Total is within limit
            # 2. At least one fish is caught
            if total <= total_allowed and (trout + pike + pickerel) > 0:
                # Valid combination! Add to output
                output_text = output_text + str(trout) + " Brown Trout, "
                output_text = output_text + str(pike) + " Northern Pike, "
                output_text = output_text + str(pickerel) + " Yellow Pickerel\n"
                valid_count = valid_count + 1
            
            pickerel = pickerel + 1
        pike = pike + 1
    trout = trout + 1

# Step 4: Output all results
print(output_text[:-1])  # [:-1] removes the last newline character
print("Number of ways to catch fish: " + str(valid_count))


# ============================================================================
# PROBLEM 9: CCC '00 J2 - 9966 (Rotatable Numbers)
# Goal: Count numbers that look the same when rotated 180 degrees
# When rotated: 0→0, 1→1, 6→9, 8→8, 9→6
# Other digits (2,3,4,5,7) can't be rotated
# Also, the digits appear in reverse order when rotated
# ============================================================================

# Step 1: Read the range to check
m = int(input())  # Start of range
n = int(input())  # End of range

# Step 2: Count rotatable numbers
count = 0

# Step 3: Check each number in the range
num = m
while num <= n:
    # Convert the number to a string so we can look at each digit
    num_str = str(num)
    
    # Build what the number would look like when rotated
    rotated = ""
    valid = True  # Assume it's valid until we find a bad digit
    
    # Process digits from right to left (rotation reverses order)
    i = len(num_str) - 1  # Start at last digit
    while i >= 0:
        digit = num_str[i]  # Get the digit at position i
        
        # Convert this digit to its rotated version
        if digit == '0':
            rotated = rotated + '0'  # 0 stays 0
        elif digit == '1':
            rotated = rotated + '1'  # 1 stays 1
        elif digit == '6':
            rotated = rotated + '9'  # 6 becomes 9
        elif digit == '8':
            rotated = rotated + '8'  # 8 stays 8
        elif digit == '9':
            rotated = rotated + '6'  # 9 becomes 6
        else:
            # This digit can't be rotated (2,3,4,5,7)
            valid = False
            break  # Stop checking this number
        
        # Move to previous digit
        i = i - 1
    
    # Check if the rotated version equals the original
    if valid and rotated == num_str:
        count = count + 1  # This number is rotatable!
    
    # Move to next number
    num = num + 1

# Step 4: Output the count
print(count)


# ============================================================================
# PROBLEM 10: CCC '06 J2 - Roll the Dice
# Goal: Count how many ways two dice can sum to 10
# First die has sides 1,2,3,...,m
# Second die has sides 1,2,3,...,n
# ============================================================================

# Step 1: Read the number of sides on each die
m = int(input())  # First die can show 1 to m
n = int(input())  # Second die can show 1 to n

# Step 2: Count all ways to get sum of 10
ways = 0  # Counter for valid combinations

# Step 3: Try every possible combination
die1 = 1  # Start with first die showing 1
while die1 <= m:  # Keep going while die1 is valid
    die2 = 1  # Start with second die showing 1
    while die2 <= n:  # Keep going while die2 is valid
        # Check if this combination sums to 10
        if die1 + die2 == 10:
            ways = ways + 1  # Found a valid combination!
        
        # Try next value for die2
        die2 = die2 + 1
    
    # Try next value for die1
    die1 = die1 + 1

# Step 4: Output with correct grammar
# Use "way" for 1, "ways" for any other number
if ways == 1:
    print("There is 1 way to get the sum 10.")
else:
    print("There are " + str(ways) + " ways to get the sum 10.")


# ============================================================================
# PROBLEM 11: CCC '05 J2 - RSA Numbers
# Goal: Count how many numbers have exactly 4 divisors
# A divisor is a number that divides evenly (no remainder)
# Example: 10 has divisors 1,2,5,10 (so 4 divisors total)
# ============================================================================

# Step 1: Read the range to check
start = int(input())  # First number to check
end = int(input())    # Last number to check

# Step 2: Count RSA numbers in the range
rsa_count = 0  # Counter for RSA numbers

# Step 3: Check each number in the range
num = start
while num <= end:
    # Count how many divisors this number has
    divisor_count = 0
    
    # Check every possible divisor from 1 to num
    divisor = 1
    while divisor <= num:
        # Check if divisor divides evenly into num
        # The % operator gives the remainder after division
        if num % divisor == 0:
            # No remainder means it divides evenly!
            divisor_count = divisor_count + 1
        divisor = divisor + 1
    
    # Check if this is an RSA number (exactly 4 divisors)
    if divisor_count == 4:
        rsa_count = rsa_count + 1
    
    # Move to next number
    num = num + 1

# Step 4: Output the result
print("The number of RSA numbers between " + str(start) + " and " + str(end) + " is " + str(rsa_count))


# ============================================================================
# PROBLEM 12: CCC '08 J2 - Do the Shuffle
# Goal: Simulate a music player with 4 buttons that rearrange songs
# Button 1: Move first song to end
# Button 2: Move last song to start  
# Button 3: Swap first two songs
# Button 4: Stop and output playlist
# ============================================================================

# Start with initial playlist as a string with spaces between songs
playlist = "A B C D E"

while True:
    # Read which button is pressed and how many times
    button = int(input())
    presses = int(input())
    
    # Button 4 means stop and output
    if button == 4:
        print(playlist)
        break  # Exit the program
    
    # Apply the button operation 'presses' times
    i = 0
    while i < presses:
        if button == 1:
            # Move first song to end: "A B C D E" becomes "B C D E A"
            # This is complex without lists, so we parse the string manually
            
            # Find the first song and the rest
            songs = ""
            song_count = 0
            first_song = ""
            other_songs = ""
            
            # Go through each character in the playlist
            current_song = ""
            for char in playlist:
                if char != ' ':  # If it's not a space, it's part of a song name
                    current_song = current_song + char
                else:  # We hit a space, so we finished reading a song
                    if song_count == 0:
                        # This is the first song
                        first_song = current_song
                    else:
                        # This is not the first song
                        if other_songs != "":
                            other_songs = other_songs + " "
                        other_songs = other_songs + current_song
                    song_count = song_count + 1
                    current_song = ""  # Reset for next song
            
            # Don't forget the last song (no space after it)
            if current_song != "":
                if song_count == 0:
                    first_song = current_song
                else:
                    if other_songs != "":
                        other_songs = other_songs + " "
                    other_songs = other_songs + current_song
            
            # Rebuild playlist with first song at end
            playlist = other_songs + " " + first_song
            
        elif button == 2:
            # Move last song to start: "A B C D E" becomes "E A B C D"
            
            # Find position of last space
            last_space_pos = -1
            k = 0
            for char in playlist:
                if char == ' ':
                    last_space_pos = k  # Remember this space position
                k = k + 1
            
            # Extract last song and everything before it
            last_song = playlist[last_space_pos + 1:]  # Everything after last space
            first_part = playlist[:last_space_pos]     # Everything before last space
            
            # Rebuild playlist with last song at start
            playlist = last_song + " " + first_part
            
        elif button == 3:
            # Swap first two songs: "A B C D E" becomes "B A C D E"
            
            # Find position of first space
            first_space = 0
            k = 0
            for char in playlist:
                if char == ' ':
                    first_space = k
                    break  # Stop looking
                k = k + 1
            
            # Find position of second space
            second_space = 0
            k = first_space + 1  # Start after first space
            while k < len(playlist):
                if playlist[k] == ' ':
                    second_space = k
                    break  # Stop looking
                k = k + 1
            
            # Extract the parts
            first_song = playlist[:first_space]           # Before first space
            second_song = playlist[first_space + 1:second_space]  # Between spaces
            rest = playlist[second_space:]                # After second space
            
            # Rebuild with first two swapped
            playlist = second_song + " " + first_song + rest
        
        # Increment counter for next press
        i = i + 1


# ============================================================================
# PROBLEM 13: CCC '05 J1 - The Cell Sell
# Goal: Compare two cell phone plans and find which is cheaper
# Plan A: 100 free daytime minutes, then 25¢ per minute
# Plan B: 250 free daytime minutes, then 45¢ per minute
# ============================================================================

# Step 1: Read how many minutes the person uses
daytime = int(input())  # Daytime minutes
evening = int(input())  # Evening minutes
weekend = int(input())  # Weekend minutes

# Step 2: Calculate Plan A cost (all amounts in cents to avoid decimals)
# Daytime: First 100 minutes are free
if daytime > 100:
    # They used more than 100 minutes, so charge for the extra
    plan_a_day = (daytime - 100) * 25  # 25 cents per extra minute
else:
    # They used 100 or fewer minutes, so it's free
    plan_a_day = 0

# Evening: 15 cents per minute (no free minutes)
plan_a_evening = evening * 15

# Weekend: 20 cents per minute
plan_a_weekend = weekend * 20

# Total for Plan A
plan_a_total = plan_a_day + plan_a_evening + plan_a_weekend

# Step 3: Calculate Plan B cost
# Daytime: First 250 minutes are free
if daytime > 250:
    # They used more than 250 minutes, so charge for the extra
    plan_b_day = (daytime - 250) * 45  # 45 cents per extra minute
else:
    # They used 250 or fewer minutes, so it's free
    plan_b_day = 0

# Evening: 35 cents per minute
plan_b_evening = evening * 35

# Weekend: 25 cents per minute
plan_b_weekend = weekend * 25

# Total for Plan B
plan_b_total = plan_b_day + plan_b_evening + plan_b_weekend

# Step 4: Convert cents to dollars for display
# Divide by 100 because 100 cents = 1 dollar
plan_a_dollars = plan_a_total / 100
plan_b_dollars = plan_b_total / 100

# Step 5: Output the costs
print("Plan A costs " + str(plan_a_dollars))
print("Plan B costs " + str(plan_b_dollars))

# Step 6: Determine and output which plan is cheaper
if plan_a_total < plan_b_total:
    print("Plan A is cheapest.")
elif plan_b_total < plan_a_total:
    print("Plan B is cheapest.")
else:
    # They cost the same
    print("Plan A and B are the same price.")


# ============================================================================
# PROBLEM 14: CCC '03 J2 - Picture Perfect
# Goal: Arrange square photos into a rectangle with minimum perimeter
# Perimeter = 2 * (width + height)
# Best arrangement is as close to a square as possible
# ============================================================================

while True:
    # Read number of photos
    c = int(input())
    
    # Check if we should stop (0 means quit)
    if c == 0:
        break
    
    # Find the best rectangle dimensions
    # Start with worst case: 1 x c (a long thin rectangle)
    best_width = 1
    best_height = c
    
    # Try all possible widths
    width = 1
    # We only need to check up to square root of c
    # because larger widths would give us dimensions we already checked
    while width * width <= c:
        # Check if this width divides evenly into c
        if c % width == 0:
            # Calculate the corresponding height
            height = c // width  # // means integer division
            
            # Check if this is better than our current best
            # Better means width and height are closer together
            if height - width < best_height - best_width:
                best_width = width
                best_height = height
        
        # Try next width
        width = width + 1
    
    # Calculate the perimeter of the best rectangle
    perimeter = 2 * (best_width + best_height)
    
    # Output the result
    print("Minimum perimeter is " + str(perimeter) + " with dimensions " + str(best_width) + " x " + str(best_height))