-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathP1216B.java
87 lines (78 loc) · 2.5 KB
/
P1216B.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Scanner;
public class P1216B
{
//implementation of HashMap
Map<Integer,Integer> map = new HashMap<Integer,Integer>();
int n;
public static void main(String[] args)
{
P1216B sv = new P1216B();
sv.createMap();
sv.sortByValue(false);
}
//method to add elements in the HashMap
void createMap()
{
Scanner entrada = new Scanner(System.in);
n= entrada.nextInt();
for (int i = 1; i <= n; i++){
map.put(i,entrada.nextInt());
}
//printMap(map);
}
//sort elements by values
void sortByValue(boolean order)
{
//convert HashMap into List
List<Entry<Integer,Integer>> list = new LinkedList<Entry<Integer,Integer>>(map.entrySet());
//sorting the list elements
Collections.sort(list, new Comparator<Entry<Integer,Integer>>()
{
public int compare(Entry<Integer,Integer> o1, Entry<Integer,Integer> o2)
{
//compare two object and return an integer
if (order) return o1.getValue().compareTo(o2.getValue());
else return o2.getValue().compareTo(o1.getValue());
}
});
//prints the sorted HashMap
Map<Integer,Integer> sortedMap = new LinkedHashMap<Integer,Integer>();
for (Entry<Integer,Integer> entry : list) sortedMap.put(entry.getKey(), entry.getValue());
printMap(sortedMap);
}
//method for printing the elements
public void printMap(Map<Integer,Integer> map)
{
int total=0;
int key=0;
int value=0;
int test=0;
for (Entry<Integer,Integer> entry : map.entrySet())
{
int sum=entry.getValue()*(test)+1;
total+= sum;
//System
if(entry.getValue()>=value && entry.getKey()>key) {
value=entry.getValue();
key=entry.getKey();
}
test++;
}
System.out.println(total);
int cont=1;
for (Entry<Integer,Integer> entry : map.entrySet())
{
if(cont != n)System.out.print(entry.getKey()+" ");
else System.out.print(entry.getKey());
cont++;
}
}
}