Skip to content

Instantly share code, notes, and snippets.

@jorovipe97
Created August 17, 2017 18:37
Show Gist options
  • Save jorovipe97/3f611b62b91c69d486a6480a60b11553 to your computer and use it in GitHub Desktop.
Save jorovipe97/3f611b62b91c69d486a6480a60b11553 to your computer and use it in GitHub Desktop.
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class HelloWorld{
static int bonApetit(int n, int k, int b, int[] arr) {
int totalCost = 0;
int bactual = 0;
// Cuanto costo la cena
for (int i=0; i < n; i++) {
if (i != k)
totalCost += arr[i];
}
bactual = totalcost/2;
bactual += arr[k]; // Cuanto debe brian cobrar a anna
int diff = b - bactual;
boolean sePaso = (diff == 0);
if (sePaso) {
return diff;
} else {
return -1;
}
}
public static void main (String []args) throws IOException {
//System.out.println("Hello World");
BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
String line;
String[] l;
int[] productCosts;
line = bf.readLine(); //para leer lineas, devuelve strings
l = line.split(" ");
int n = Integer.parseInt(l[0]);
int k = Integer.parseInt(l[1]);
line = bf.readLine();
l = line.split(" ");
productCosts = new int[n];
for (int i = 0; i < n; i++) {
productCosts[i] = Integer.parseInt(l[i]);
}
line = bf.readLine();
int b = Integer.parseInt(line);
int res = bonApetit(n, k, b, productCosts);
if (res == -1) {
System.out.println("Bon Appetit");
} else {
System.out.println(res);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment