[BOJ] - 13305. 주유소[그리디] - c++

ha·2022년 1월 29일
0

BOJ

목록 보기
15/28

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

c++풀이(그리디)

최초 기름값, 거리 반영 후 도시 한칸 통과시 마다 바로 이전까지 최소 기름값과 현재 기름값 비교하면서 다음 도시로 이동한다.

long long price[100000];
long long dist[100000];
int N;
long long tot;
long long bestp;

int main()
{
  cin>>N;
  for(int i=0;i<N-1;i++)
  {
    cin>> dist[i]; 
  }
  for(int i=0;i<N;i++)
  {
      cin>>price[i];
  }
  
  tot=price[0]*dist[0];
  bestp=price[0];
  
  for(int i=1;i<N-1;i++)
  {
      if(price[i]<bestp) bestp=price[i];
      tot+=(bestp*dist[i]);
  }
  cout<<tot;
  return 0;
}

0개의 댓글