Accepted
Code
#include<bits/stdc++.h>
using namespace std;
int main() {
int t;
cin>>t;
while (t--) {
int n;
cin >> n;
vector<long long> buyPrices(n);
vector<long long> sellPrices(n);
for (int i = 0; i < n; i++) {
cin >> buyPrices[i];
}
for (int i = 0; i < n; i++) {
cin >> sellPrices[i];
}
vector<long long> maxSellPrice(n);
maxSellPrice[n - 1] = sellPrices[n - 1];
for (int i = n - 2; i >= 0; i--) {
if (sellPrices[i] > maxSellPrice[i + 1]) {
maxSellPrice[i] = sellPrices[i];
} else {
maxSellPrice[i] = maxSellPrice[i + 1];
}
}
long long totalProfit = 0;
for (int i = 0; i < n; i++) {
if (maxSellPrice[i] > buyPrices[i]) {
totalProfit += maxSellPrice[i] - buyPrices[i];
}
}
cout << totalProfit << "\n";
}
return 0;
}
Information
- Submit By
- Type
- Submission
- Problem
- P1228 Business Strategy
- Contest
- LUCC Presents Intra LU Junior Programming Contest - Replay
- Language
- C++11 (G++ 13.2.0)
- Submit At
- 2025-09-02 17:33:21
- Judged At
- 2025-09-02 17:33:21
- Judged By
- Score
- 100
- Total Time
- 96ms
- Peak Memory
- 2.812 MiB