[백준] 13305번: 주유소

2021. 2. 14. 14:09Algorithm/백준

 

13305번: 주유소

표준 입력으로 다음 정보가 주어진다. 첫 번째 줄에는 도시의 개수를 나타내는 정수 N(2 ≤ N ≤ 100,000)이 주어진다. 다음 줄에는 인접한 두 도시를 연결하는 도로의 길이가 제일 왼쪽 도로부터 N-1

www.acmicpc.net

#include<iostream>
#include<vector>
using namespace std;

long long solution(vector<long long>&dist, vector<long long>&cost) {
	long long total = 0; int idx = 0;
	total += cost[0] * dist[1];
	for (int i = 1;i < dist.size() - 1;i++) {
		if (cost[i] < cost[idx]) {
			total += cost[i] * dist[i + 1];
			idx = i;
		}
		else total += cost[idx] * dist[i + 1];
	}
	return total;
}

int main() {
	int n;
	cin >> n;

	vector<long long>dist(n, 0);
	vector<long long>cost(n, 0);

	for (int i =1;i < n ;i++) {
		cin >> dist[i];
	}
	for (int i = 0;i < n;i++) {
		cin >> cost[i];
	}

	cout<<solution(dist, cost);
}