int a[N]; char s[N]; vector<int> ve[N]; voidsolve(){ int n, k; cin >> n >> k; for (int i = 1; i <= n; i++) cin >> a[i]; cin >> (s + 1); int cnt = 0; for (int i = 1; i <= n; i++) { if (s[i] == s[i-1]) ve[cnt].push_back(a[i]); else { cnt++; ve[cnt].push_back(a[i]); } } for (int i = 1; i <= cnt; i++) sort(ve[i].begin(), ve[i].end(), greater<int>()); ll ans = 0; for (int i = 1; i <= cnt; i++) { for (int j = 0; j < min(k, (int)ve[i].size()); j++) ans += ve[i][j]; } cout << ans << endl; }
int sum[5250][5250], n; intget(int x1, int y1, int x2, int y2){ return sum[x2][y2]+sum[x1-1][y1-1]-sum[x1-1][y2]-sum[x2][y1-1]; } boolcheck(int x){ for (int i = 1; i <= n/x; i++) { for (int j = 1; j <= n/x; j++) { int x1 = x*(i-1)+1, y1 = x*(j-1)+1; int x2 = i*x, y2 = j*x; int ans = get(x1, y1, x2, y2); if (ans != 0 && ans != x*x) returnfalse; } } returntrue; } voidsolve(){ cin >> n; for (int i = 1; i <= n; i++) { for (int j = 1; j <= n/4; j++) { char c; cin >> c; int now = 0; if (c >= 'A') now = 10 + c - 'A'; else now = c - '0'; for (int k = 0; k < 4; k++) { sum[i][4*(j-1)+k+1] = now >> (4-k-1) & 1; } } } for(int i = 1; i <= n; i++) { for (int j = 1; j <= n; j++) { sum[i][j] += sum[i - 1][j] + sum[i][j - 1] - sum[i - 1][j - 1]; } } int ans = 0; for (int i = 1; i <= sqrt(n); i++) { if (n % i == 0) { if (check(i)) ans = max(ans, i); if (check(n/i)) ans = max(ans, n/i); } } cout << ans << endl; }