https://www.acmicpc.net/problem/11399

 

11399번: ATM

첫째 줄에 사람의 수 N(1 ≤ N ≤ 1,000)이 주어진다. 둘째 줄에는 각 사람이 돈을 인출하는데 걸리는 시간 Pi가 주어진다. (1 ≤ Pi ≤ 1,000)

www.acmicpc.net

 

그리디알고리즘입니다.

 

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Scanner;
class Withdrawer implements Comparable<Withdrawer>{
private int time;
public Withdrawer(int time) {
this.time = time;
}
public int getTime() {
return this.time;
}
public void setTime(int time) {
this.time = time;
}
@Override
public int compareTo(Withdrawer other) {
return this.time - other.time;
}
@Override
public String toString() {
return "toString:"+this.time;
}
}
public class Main {
public static int N;
public static List<Withdrawer> withdrawerlist = new ArrayList<Withdrawer>();
public static int prev=0,sum=0;
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
N = sc.nextInt();
for(int i=0;i<N;i++) {
withdrawerlist.add(new Withdrawer(sc.nextInt()));
}
Collections.sort(withdrawerlist);
for(int i=0;i<N;i++) {
prev += withdrawerlist.get(i).getTime();
sum +=prev;
}
System.out.println(sum);
}
}

+ Recent posts