Advertisement
ec1117

Untitled

Jul 2nd, 2020
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.28 KB | None | 0 0
  1. import java.util.*;
  2. import java.io.*;
  3. import java.io.BufferedReader;
  4. import java.awt.Point;
  5.  
  6. public class cbarn{
  7. public static final String TASKNAME = "cbarn";
  8. public static long mod= 1000000007;
  9. public static Debug db;
  10.  
  11. public static void main(String[] args) throws IOException {
  12. InputReader in = new InputReader(System.in);
  13. PrintWriter out = new PrintWriter(System.out);
  14. // InputReader in = new InputReader(new FileInputStream(TASKNAME+".in"));
  15. // PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(TASKNAME+".out")));
  16. Autocompletion solver = new Autocompletion();
  17. db=new Debug(System.getSecurityManager()==null);
  18. solver.solve(1, in, out);
  19. out.close();
  20. }
  21. static class Autocompletion {
  22. int rot;
  23. int N, K;
  24. int MAXN=1010;
  25. int MAXK=10;
  26. int a[]=new int[MAXN];
  27. long A[]=new long[MAXN];
  28. long DP[][]=new long[MAXK][MAXN];
  29. long WSUM[][]=new long[MAXN][MAXN];
  30. long INF=Long.MAX_VALUE;
  31. public void solve(int testNumber, InputReader in, PrintWriter out) {
  32. N=in.nextInt();
  33. K=in.nextInt();
  34. a=in.nextIntArr(N);
  35. for(int i=0;i<N;i++) {
  36. A[i]=a[N-1-i];
  37. }
  38. for (int i = 0; i < N; i++) {
  39. long sm = 0;
  40. for (int j = 1; j <= N; j++) {
  41. WSUM[i][j] = WSUM[i][j - 1] + sm;
  42. sm += A[madd(i, j - 1)];
  43. }
  44. }
  45. long result = INF;
  46. for(int i=0;i<DP.length;i++) {
  47. Arrays.fill(DP[i], Long.MAX_VALUE);
  48. }
  49.  
  50. // memset(DP, 0x3F, sizeof(DP));
  51. DP[0][N] = 0;
  52. for (rot = 0; rot < N; rot++) {
  53. for (int i = 1; i <= K; i++) {
  54. solve(i, 0, N, 1, N + 1);
  55. }
  56. result = Math.min(result, DP[K][0]);
  57. }
  58. out.println(result);
  59. }
  60. int madd(int x, int y) {
  61. x += y;
  62. if (x >= N) {
  63. x -= N;
  64. }
  65. return x;
  66. }
  67.  
  68. long wsum(int a, int b) {
  69. return WSUM[madd(a, rot)][madd(b, N - a)];
  70. }
  71.  
  72. void solve(int k, int ia, int ib, int ja, int jb) {
  73. if (ia == ib) {
  74. return;
  75. }
  76.  
  77. int i = (ia + ib) / 2;
  78. int arg_j = -1;
  79.  
  80. DP[k][i] = INF;
  81. for (int j = Math.max(i + 1, ja); j < jb; j++) {
  82. long v = wsum(i, j) + DP[k - 1][j];
  83. if (v < DP[k][i]) {
  84. arg_j = j;
  85. DP[k][i] = v;
  86. }
  87. }
  88.  
  89. solve(k, ia, i, ja, arg_j + 1);
  90. solve(k, i + 1, ib, arg_j, jb);
  91. }
  92. }
  93. static class Pair implements Comparable<Pair>{
  94. int x;
  95. int y;
  96. Pair(int a, int b){
  97. x=a;
  98. y=b;
  99. }
  100. @Override
  101. public int compareTo(Pair arg0) {
  102. if(arg0.x!=x)return x-arg0.x;
  103. return y-arg0.y;
  104. }
  105. }
  106. static class Triple implements Comparable<Triple>{
  107. int x;
  108. int y;
  109. int z;
  110. Triple(int a, int b, int c){
  111. x=a;
  112. y=b;
  113. z=c;
  114. }
  115. @Override
  116. public int compareTo(Triple arg0) {
  117. if(arg0.x!=x)return x-arg0.x;
  118. return y-arg0.y;
  119. }
  120. }
  121.  
  122. static class InputReader {
  123. public BufferedReader reader;
  124. public StringTokenizer tokenizer;
  125.  
  126. public InputReader(InputStream stream) {
  127. reader = new BufferedReader(new InputStreamReader(stream), 32768);
  128. tokenizer = null;
  129. }
  130.  
  131. public String next() {
  132. while (tokenizer == null || !tokenizer.hasMoreTokens()) {
  133. try {
  134. tokenizer = new StringTokenizer(reader.readLine());
  135. } catch (IOException e) {
  136. throw new RuntimeException(e);
  137. }
  138. }
  139. return tokenizer.nextToken();
  140. }
  141.  
  142. public int nextInt() {
  143. return Integer.parseInt(next());
  144. }
  145. public long nextLong() {
  146. return Long.parseLong(next());
  147. }
  148. public int[] nextIntArr(int n) {
  149. int arr[]=new int[n];
  150. for(int i=0;i<n;i++) {
  151. arr[i]=this.nextInt();
  152. }
  153. return arr;
  154. }
  155. }
  156. public static class Debug {
  157. private boolean allowDebug;
  158.  
  159. public Debug(boolean allowDebug) {
  160. this.allowDebug = allowDebug;
  161. }
  162.  
  163. private void outputName(String name) {
  164. System.out.print(name + " = ");
  165. }
  166.  
  167. public void debug(String name, int x) {
  168. if (!allowDebug) {
  169. return;
  170. }
  171.  
  172. outputName(name);
  173. System.out.println("" + x);
  174. }
  175.  
  176. public void debug(String name, long x) {
  177. if (!allowDebug) {
  178. return;
  179. }
  180. outputName(name);
  181. System.out.println("" + x);
  182. }
  183.  
  184. public void debug(String name, double x) {
  185. if (!allowDebug) {
  186. return;
  187. }
  188. outputName(name);
  189. System.out.println("" + x);
  190. }
  191.  
  192. public void debug(String name, int[] x) {
  193. if (!allowDebug) {
  194. return;
  195. }
  196. outputName(name);
  197. System.out.println(Arrays.toString(x));
  198. }
  199.  
  200. public void debug(String name, long[] x) {
  201. if (!allowDebug) {
  202. return;
  203. }
  204. outputName(name);
  205. System.out.println(Arrays.toString(x));
  206. }
  207.  
  208. public void debug(String name, double[] x) {
  209. if (!allowDebug) {
  210. return;
  211. }
  212. outputName(name);
  213. System.out.println(Arrays.toString(x));
  214. }
  215.  
  216. public void debug(String name, Pair[] x) {
  217. if (!allowDebug) {
  218. return;
  219. }
  220. outputName(name);
  221. StringBuilder sb = new StringBuilder("[");
  222. int cnt=0;
  223. for(Pair y:x) {
  224. sb.append('('+y.x+","+y.y+')');
  225. if (cnt != x.length-1)sb.append(", ");
  226. cnt++;
  227. }
  228. System.out.println(sb.append("]").toString());
  229. }
  230.  
  231. public void debug(String name, Object x) {
  232. if (!allowDebug) {
  233. return;
  234. }
  235. outputName(name);
  236. System.out.println("" + x);
  237. }
  238.  
  239. public void debug(String name, Object... x) {
  240. if (!allowDebug) {
  241. return;
  242. }
  243. outputName(name);
  244. System.out.println(Arrays.deepToString(x));
  245. }
  246. }
  247. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement