forked from daiwb/Algorithm
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathC.cpp
More file actions
63 lines (56 loc) · 1.26 KB
/
Copy pathC.cpp
File metadata and controls
63 lines (56 loc) · 1.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#include <iostream>
#include <vector>
using namespace std;
#define REP(i,n) for(int i=0;i<(n);++i)
#define FOR(i,a,b) for(int i=(a);i<=(b);++i)
typedef long long LL;
void run() {
int r, k, n;
cin >> r >> k >> n;
vector<LL> mm(n);
REP(i,n) {
cin >> mm[i];
}
vector<LL> money(n, 0);
vector<int> next(n);
REP(i,n) {
REP(j,n) {
if (money[i] + mm[(i + j) % n] > k) break;
money[i] += mm[(i + j) % n];
next[i] = (i + j + 1) % n;
}
}
LL total = 0;
int idx = 0;
vector<LL> dd;
vector<int> vv(n, -1);
REP(i,r) {
if (vv[idx] != -1) {
int cnt = i - vv[idx];
LL s = 0;
REP(j,cnt) s += dd[vv[idx] + j];
LL left = r - i;
LL c = left / cnt;
total += c * s;
REP(j,left - cnt * c) {
total += dd[vv[idx] + j];
}
cout << total << endl;
return;
}
vv[idx] = i;
dd.push_back(money[idx]);
total += money[idx];
idx = next[idx];
}
cout << total << endl;
}
int main() {
int kase;
cin >> kase;
FOR(k,1,kase) {
cout << "Case #" << k << ": ";
run();
}
return 0;
}