Algorithm
Problem Name: beecrowd | 2951
Problem Link: https://www.beecrowd.com.br/judge/en/problems/view/2951
The Return of The King
By Samuel Eduardo da Silva, IFSULDEMINAS/UFF Brazil
Timelimit: 1
Frodo and Sam are about to throw the ring on the Mountain of Perdition, but Gollum disturbs them.
A little pause in history. Lord of the Rings, besides being one of humanity's greatest literary and cinematographic classics, is a story that makes clear the value of friendship. Give value to good friendships :) Resume.
Gollum is an unhappy and unbearable being. For Frodo and Sam to get through it, they need to recite runes that sing friendship. Each rune is represented by a letter of the alphabet, and indicates a quantity of friendship that it emits, being able to be positive or negative (yes, there are runes that represent the most friendships).
Given the amount of friendship necessary to defeat Gollum, a list of runes and their respective values of friendship, and the runes that Sam and Frodo recited, give the ultimate worth of friendship that Frodo and Sam succeeded in and whether or not it was possible to defeat Gollum.
Input
The first line of the entry consists of two integers N(1 <= N) and G(G <= 100), indicating, respectively, the number of runes in existence, and the amount of friendship necessary to defeat Gollum. The next N lines are composed of a character Ri('A' <= Ri <= 'Z') and an integer Vi(-100 <= Vi <= 100), indicating, respectively, the rune and the value of friendship that it adds. The next line is started by an integer X, indicating the number of runes recited by Frodo and Sam. The last line of the entry is composed of X characters, indicating the runes recited by Frodo and Sam.
Output
The first line of the output should contain the amount of friendship value. The second line should contain one of the following messages:
● "My precioooous", if Gollum wins;
● "You shall pass!" If Frodo and Sam win.
Input Sample | Output Sample |
8 10 |
10 |
Code Examples
#1 Code Example with C Programming
Code -
C Programming
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(int argc, char **argv)
{
char c;
int n, m, ans, x, i;
int run[200] = { 0 };
scanf("%d %d%*c", &n, &m);
while (n--)
scanf("%c %d%*c", &c, &x), run[c] = x;
ans = 0;
scanf("%d%*c", &x);
while (x--)
scanf("%c%*c", &c), ans += run[c];
printf("%d\n%s\n", ans, ans >= m ? "You shall pass!" : "My precioooous");
return 0;
}
Copy The Code &
Try With Live Editor
Input
Output
#2 Code Example with Javascript Programming
Code -
Javascript Programming
var input = require('fs').readFileSync('/dev/stdin', 'utf8');
var lines = input.split('\n');
var prompt = function(texto) { return lines.shift();};
const [ammount, needed] = prompt().split(" ").map(Number);
const runes = new Map();
for (let i = 0; i < ammount; i++) {
let rune = prompt().split(" ");
rune[1] = parseInt(rune[1]);
runes.set(rune[0], rune[1]);
}
prompt();
var recited = prompt().split(" ");
var sum = 0;
for (let item of recited) {
sum += runes.get(item);
}
console.log(sum);
if (sum >= needed) {
console.log("You shall pass!");
} else {
console.log("My precioooous");
}
Copy The Code &
Try With Live Editor
Input
Output
#3 Code Example with Python Programming
Code -
Python Programming
n, m = map(int, input().split())
run = {}
for i in range(n):
c, x = input().split()
run[c] = int(x)
aux = 0
x = int(input())
s = input().split()
for c in s:
aux += run[c]
print(aux)
print("You shall pass!") if aux >= m else print("My precioooous")
Copy The Code &
Try With Live Editor
Input
Output