Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <algorithm>
- using namespace std;
- struct agent
- {
- int age;
- int danger;
- };
- int compare(agent a, agent b)
- { return a.age < b.age; }
- int main( )
- {
- FILE *in = fopen("input.txt", "rt"),
- *out = fopen("output.txt", "wt");
- int n;
- fscanf(in, "%d", &n);
- agent potok[n];
- for (int i = 0; i < n; ++i)
- {
- fscanf(in, "%d %d", &potok[i].age, &potok[i].danger);
- }
- sort(potok, potok + n, compare);
- int d[n];
- d[1] = potok[1].danger;
- d[2] = d[1] + potok[2].danger;
- for (int i = 3; i < n; ++i)
- {
- d[i] = min(d[i-1], d[i-2]) + potok[i].danger;
- }
- fprintf(out, "%d", d[n - 1]);
- fclose(in);
- fclose(out);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment