본문 바로가기
알고리즘 & 자료구조/백준

백준 11501

by 신재권 2023. 7. 24.
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.StringTokenizer;

class Main11501 {

   public static void main(String[] args) throws Exception {
      BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
      int T = Integer.parseInt(br.readLine());

      while (T-- > 0) {
         int N = Integer.parseInt(br.readLine());
         int[] price = new int[N];

         StringTokenizer st = new StringTokenizer(br.readLine());
         for (int i = 0; i < N; i++) {
            price[i] = Integer.parseInt(st.nextToken());
         }

         long answer = 0;
         long max = 0;

         for (int i = N - 1; i >= 0; i--) {
            if (price[i] > max) {
               max = price[i];
               continue;
            }

            answer += max - price[i];
         }

         System.out.println(answer);
      }

   }
}

'알고리즘 & 자료구조 > 백준' 카테고리의 다른 글

백준 1931  (0) 2023.07.25
백준 1080  (0) 2023.07.24
백준 21314  (0) 2023.07.23
백준 16953  (0) 2023.07.20
백준 20365  (0) 2023.07.19