Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- import java.io.*;
- import java.math.*;
- class Solution
- {
- public static void main(String args[])
- {
- Scanner in = new Scanner(System.in);
- int N = in.nextInt();
- if (in.hasNextLine())
- {
- in.nextLine();
- }
- int sum = 0;
- String books[] = new String[N];
- int time[] = new int[N];
- for (int i = 0; i < N; i++)
- {
- books[i] = in.nextLine();
- time[i] = Integer.parseInt(books[i].split(",")[1]);
- }
- boolean sorted = false;
- int totalTime = 0;
- while(!sorted)
- {
- sorted = true;
- for(int i = 1; i < books.length; i++)
- {
- if(stringsComparator(books[i-1], books[i]) == 1)
- {
- String tmp = books[i];
- books[i] = books[i-1];
- books[i-1] = tmp;
- totalTime+=Integer.parseInt(books[i].split(",")[1]);
- totalTime+=Integer.parseInt(books[i-1].split(",")[1]);
- sorted = false;
- }
- }
- }
- System.out.println(totalTime);
- }
- public static int stringsComparator(String s1, String s2) // returns 1 if s1 greater, -1 si s1 lesser ?(is this a word?), 0 si equals
- {
- for(int i = 0; i < Math.min(s1.length(), s2.length()); i++)
- {
- if(s1.charAt(i) > s2.charAt(i))
- return 1;
- else if(s1.charAt(i) < s2.charAt(i))
- return -1;
- }
- return 0;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement